I have this userContext file:
import React from 'react'
const userContext = React.createContext({ loggedInUser: {} })
export { userContext }
I provide correctly a value in App:
import { userContext } from './../contexts/UserContext'
class App extends Component {
//...
componentDidMount = () => {
this.authServices
.isLoggedIn()
.then(response => this.setState({ loggedInUser: response.data })
.catch(() => this.setUser(undefined))
}
return (
<>
<userContext.Provider value={this.state.loggedInUser}>
<Navigation/>
</userContext.Provider>
</>
)
}
And can access that value in any nested component such as in Navigation:
/* ... */
<userContext.Consumer>
{value => value && <h1>Hi! {value.username}</h1>}
</userContext.Consumer>
/* ... */
How can I change from these nested components such as Navigation the value from the loggedInUser property in the context?
I don't find the way to attach methods to the context and use them, for example, when user logs out in the Navigation component:
/* ... */
logOut = () => {
this.authService
.logout()
.then(() => /* modify here the loggedInUser from the context */ )
/* ... */
Please note this is a stateful component and I need a non-Hooks solution.