context API is similar to redux. It stores data in provider and we can access that data in the subcomponents.
- Provider : Where the data lives
- Consumer : where we can access the data
eg: const MyContext = React.createContext();
eg: class MyProvider extends Component {
state={
name:"Pavan kumar Dasireddy"
}
render() {
return (
<MyContext.Provider value={{
state:this.state
}}>
{this.props.children}
</MyContext.Provider>
)
}
}
eg: <MyProvider>
<div>
<p>Hey! I'm App</p>
<Family/>
</div>
</MyProvider>
eg: <MyContext.Consumer>
{(value)=> (
<p>Im {value.state.name}</p>
)}
</MyContext.Consumer>
https://engineering.musefind.com/how-to-build-a-chrome-extension-with-react-js-e2bae31747fc