Skip to content

Instantly share code, notes, and snippets.

@kylephughes
Created July 27, 2021 13:35
Show Gist options
  • Save kylephughes/7524b6d7711c9be423a35619d7b1ffc9 to your computer and use it in GitHub Desktop.
Save kylephughes/7524b6d7711c9be423a35619d7b1ffc9 to your computer and use it in GitHub Desktop.
// <App/>
export const NotificationContext = React.createContext({...})
return (
<NotificationContext.Provider value={{ notification, setNotification }}>
// any child component can consume these values directly
</NotificationContext.Provider>
)
// <Notification/>
import { useContext } from 'react'
export const Notification = (props) => {
const { notification } = useContext(NotificationContext)
}
// <FormComponent/>
import { useContext } from 'react'
export const FormComponent = (props) => {
const { setNotification } = useContext(NotificationContext)
const sendNotification = () => {
setNotification('Success')
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment