Example of using React Context for spreading Toast (Primereact Toast) showMessage functionality down the DOM tree
Spawned using Create React App
- Wrap root application node in Toaster tag
import Toaster from "./components/Toaster/Toaster";
const App = () => {
return (
<Toaster>
<div className="App">
...
</div>
</Toaster>
);
}
- Add Context.Consumer to inner component
const {showToast} = useContext(ToasterContext)
- Use showToast function to show Toast
showToast("warn", "Warning!")