I’m trying to wrap my head around the Context concept in React, I want to use it to create a reusable confirm modal with the minimum possible boilerplate (for the modal consumer), in my mind, and due to my misunderstanding of how context works (like a useState), this is what I imagine:
From the main App, you import the component and the launcher function:
import {ConfirmDialog, ShowConfirmModal} from './components/ConfirmDialog';
And inside components/ConfirmDialog.js you have:
import React, {createContext, useContext} from "react";
import {ModalDialog, Text} from "@forge/react";
const context = createContext({ isOpen: false })
export const ConfirmDialog = () => {
const _context = useContext(context)
return _context.isOpen && <ModalDialog header={_context.title} onClose={_context.onClose} closeButtonText='Confirm'>
<Text>{_context.message}</Text>
</ModalDialog>
}
export const ShowConfirmModal = (title, message, onClose) => {
const [_context, _setContext] = useContext(context)
_setContext({
message, title, onClose, isOpen: true
})
}
This will allow the consumer to add the component in the root of the document and to show it simply by calling the ShowConfirmModal function. BUT, this does not work, since I cannot use: const [_context, _setContext] = useContext(context)
And now I don’t understand what will be the best approach to solve this or even if mine is correct (with some modifications).
I’m new to React and I what I want more than a solution to the issue, is an orientation, is my reasoning correct? Or am I not "thinking in React"? (I’m a MVC dev)
2
Answers
First of all you need a provider component:
You then have to use your provider in an ancestor of all components that want to open a modal
e.g.
Now you can use your context:
Notes:
useContext(DialogContext)
there, but it must be added to the tree anywayconst useDialogContext = useContext(DialogContext)
so you don’t have to write that muchYour approach is correct, but there are few suggestions about how React context works
you can follow these steps to make some corrections
the default value should include the properties that you are accessing in your components.
`
The provider sets the values for the context
3.Use the context hook
ConfirmDialog component should use the useContext hook to get the context.
Show Confirmation Modal
Finally use that ConfrimDialogProvider in APP funtion