Skip to content

Instantly share code, notes, and snippets.

@tudorilisoi
Last active July 21, 2020 18:48
Show Gist options
  • Save tudorilisoi/103a6aa0cfb43fe61fa10f1e0c2371ef to your computer and use it in GitHub Desktop.
Save tudorilisoi/103a6aa0cfb43fe61fa10f1e0c2371ef to your computer and use it in GitHub Desktop.
import React from 'react'
//define your context
const APIContext = React.createContext(null)
// export default APIContext;
class NotesList extends Component {
//make the context accessible within this component
static contextType = APIContext;
render() {
return (<p>{this.context.username}</p>)
}
}
function App(){
//use APIContext.Provider to provide the context down the tree
return (
<APIContext.Provider value={{username:'Amy'}}>
<div>
<div>
<NotesList />
</div>
<NotesList />
</div>
</APIContext.Provider>
)
}
React.render(document.getElementById('body'), App)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment