Next.js Discord

Discord Forum

Should I use Context or Zustand maybe better way?

Unanswered
Rhinelander posted this in #help-forum
Open in Discord
RhinelanderOP
I have a blog with several buttons that trigger a newsletter modal. All these buttons are within a component called "newsletter card." Whenever I click any of these buttons, I want the newsletter modal to open, no matter where I am in the app.

3 Replies

Satin Angora
If you don't need to pass data to dialog, you don't need context or anything else. If you are using HTML <dialog> element, you can open it from anywhere using a function call assigned to the button. You can put the element on a higher branch of the layout tree, and you can open it from child elements.
An example:
export function handleClick(dialogId: string) {
  const dialog =
    document.getElementById(dialogId) as HTMLDialogElement
  dialog.showModal()
}
RhinelanderOP
Damn thanks!