Previously in order to show Dialog in React, we had to write the code below
const Component = () => {const [open, setOpen] = useState(false);return (<div><buttononClick={() => {setOpen(true);}}>open dialog</button><Dialogopen={open}onClose={() => {setOpen(false);}}/></div>);};
The code above has Dialog-Related code throughout the Function Component.
If components become complicated, readability becomes poor and maintenance becomes difficult.
This library can show React Component from anywhere to solve this issues.
This package has peer dependencies, which you need to install by yourself.
// npmnpm install react-opener react// yarnyarn add react-opener react
import { ReactOpener, ReactToastOpener } from "react-opener";const ToastStore = ReactToastOpener.createStore();const DialogStore = ReactOpener.createStore();const Container = () => {return (<div><buttontype="button"onClick={() => {ToastStore.success("toast !");}}>toast</button><buttontype="button"onClick={() => {DialogStore.open({element: ({ close }) => <YourDialog open={true} onClose={close} />,});}}>dialog</button>// Rendered here !<ReactToastOpener store={ToastStore} /><ReactOpener store={DialogStore} /></div>);};