Skip to content

Instantly share code, notes, and snippets.

@jpsierens
Last active May 30, 2020 17:57
Show Gist options
  • Save jpsierens/64add032710129b96ac8e52455a27086 to your computer and use it in GitHub Desktop.
Save jpsierens/64add032710129b96ac8e52455a27086 to your computer and use it in GitHub Desktop.
// 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>
);
}
// 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