'Getting new Context State after Context State is updated
I have a child component inside a context provider. After a child component calls an update state to the context provider, the context provider's state changes. However, this new changed value is not attainable from the child state. How would I be able to retrieve the updated value of the changed state from the Child component?
Or... is this just a bad practice and is there another way to do this?
I have a Context State in a Context Provider
AuthContextProvider=()=>{
const[data,updatedata]=React.useState(null)
AuthUpdate function(){
updateData() //updates the state
}
//... pass AuthUpdate, data to children
}
and a child component... as such
<AuthContextProvider>
<NavBar>
</NavBar>
</AuthContextProvider>
Navbar looks like this:
NavBar=()=>{
const {data,AuthUpdate}=useAuth()
onClick function(){
AuthUpdate() //data in the context changes
console.log(data) //somehow use the UPDATED data after context state changes
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
