Last active
May 30, 2020 17:57
-
-
Save jpsierens/64add032710129b96ac8e52455a27086 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Create the context for user related data | |
export const UserContext = React.createContext(); | |
const user = { | |
name: 'Jean-Pierre', | |
sex: 'Yes' | |
} | |
// provider the user context anywhere in the app | |
function App() { | |
return ( | |
<UserContext.Provider value={user}> | |
<App /> | |
</UserContext.Provider> | |
); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Somewhere deep inside the app | |
import React, { useContext } from 'react'; | |
import { UserContext } from './index' | |
export default () => { | |
const { name } = useContext(UserContext) | |
return ( | |
<h1>Hello {name}!</h1> | |
) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment