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

149
Views
How do I nest multiple React contexts of the same type

Say I want to create a modal and handle the state through Reacts Context API

export const ModalUIProvider: FC = ({ children }) => {
    const [isOpen, setIsOpen] = useState(false);

    const openModal = useCallback(() => setIsOpen(true), [setIsOpen]);

    const closeModal = useCallback(() => setIsOpen(false), [setIsOpen]);

    const toggleModal = useCallback(
        () => setIsOpen(!isOpen),
        [isOpen, setIsOpen]
    );

    const modalContext = useMemo(() => {
        return {
            isModalOpen: isOpen,
            openModal,
            closeModal,
            toggleModal,
        };
    }, [isOpen, openModal, closeModal, toggleModal]);

    return (
        <ModalContext.Provider value={modalContext}>
            {children}
        </ModalContext.Provider>
    );
};

export const useModal = () => {
    return useContext<IModalContext>(ModalContext);
};

<ModalUIProvider>
    <ParentModal />
</ModalUIProvider>

const { isModalOpen, openModal, closeModal } = useModal();

Now, within this modal I want to have a button, that upon being clicked opens another model ontop of the current one.

How can I do this, without interfering with the wrapping context?

about 4 years ago · Juan Pablo Isaza
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!