gpt4 book ai didi

javascript - React 组件用户不在状态

转载 作者:行者123 更新时间:2023-12-03 00:43:55 24 4
gpt4 key购买 nike

考虑以下因素:

 componentDidMount(){
this.props.requestUser(this.props.match.params.userID).then(res =>
{
this.pageOwner = res.user;
debugger;
})

}

为什么我的 this.pageOwner 仍然返回未定义?我一直在尝试让用户进入我的用户个人资料实际上可以访问的状态。

最佳答案

如果您尝试访问 this.pageOwner,则需要在 this.props.requestUser() 异步函数完成后执行此操作。您当前正在 componentDidMount() 中获取用户,因此您的 render() 最初会将 this.pageOwner 视为 undefined 直到异步调用完成。

此外,最好将异步调用的结果存储在 state 中,以便在填充值后组件将重新渲染。

关于javascript - React 组件用户不在状态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53328472/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com