Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

155
Views
React modal context hook fails to open more than one modal

My context for displaying modals works perfectly when used to display a single modal. However, when I try open a modal on top of a modal, nothing happens. Setting a breakpoint in showDialog reveals that the function is triggered when opening the first modal, but not for successive modals as long as the first one remains open. I do not understand this behavior. Why does it not trigger?

type DialogComponent = (onClosed: () => void) => JSX.Element;

interface DialogState {
    showDialog: (dialog: DialogComponent) => void;
    clearDialogs: () => void;
}

const DialogContext = createContext<DialogState>({
    showDialog: (_dialog) => {},
    clearDialogs: () => {},
});

export const useDialog = () => useContext(DialogContext);

export const DialogProvider = ({ children }: PropsWithChildren<{}>) => {
    const [dialogs, setDialogs] = useState<Array<DialogComponent>>([]);

    const showDialog = (dialog: DialogComponent) => {
        setDialogs([dialog, ...dialogs]);
    };

    const closeDialog = (dialog: DialogComponent) =>
        setDialogs(dialogs.filter((d) => d !== dialog));

    return (
        <>
            {dialogs.map((dialog) => (
                <div key={UniqueId()}>{dialog(() => closeDialog(dialog))}</div>
            ))}

            <DialogContext.Provider
                value={{
                    showDialog,
                    clearDialogs: () => setDialogs([]),
                }}
            >
                {children}
            </DialogContext.Provider>
        </>
    );
};
about 4 years ago · Santiago Gelvez
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!