更新上下文状态后获取新的上下文状态
我在上下文提供程序中有一个子组件。在子组件调用上下文提供程序的更新状态后,上下文提供程序的状态会发生更改。但是,无法从子状态获得这个新的更改值。我如何能够从子组件中检索更改状态的更新值?
或者...这只是一种不好的做法,还有其他方法可以做到这一点吗?
我在上下文提供程序
AuthContextProvider=()=>{
const[data,updatedata]=React.useState(null)
AuthUpdate function(){
updateData() //updates the state
}
//... pass AuthUpdate, data to children
}
和子组件中有一个上下文状态...这样的
<AuthContextProvider>
<NavBar>
</NavBar>
</AuthContextProvider>
导航栏看起来像这样:
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
}
}
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
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论