IF you want to change React context programmatically? In order to use Context, you need a Provider which takes a value, and that value could come from the state of the component and be updated
1 2 3 4 5 6 7 8 9 10 11 12 13 | class App extends React.Component { state = { isAuth: false; } componentDidMount() { APIcall().then((res) => { this.setState({isAuth: res}) // update isAuth }) } render() { <LoggedUserContext.Provider value={this.state.isAuth}> <Child /> </LoggedUserContext.Provider> } } |
If you like this question & answer and want to contribute, then write your question & answer and email to freewebmentor[@]gmail.com. Your question and answer will appear on FreeWebMentor.com and help other developers.