gpt4 book ai didi

javascript - 我如何正确使用 componentWillReceiveProps()?

转载 作者:塔克拉玛干 更新时间:2023-11-02 22:01:32 25 4
gpt4 key购买 nike

我正在尝试在收到新 Prop 后立即更新子组件。但是,我的子组件中的 componentWillReceiveProps() 在 Prop 实际更新之前被调用。看完this article我明白为什么,但这并不能解释我如何解决我的问题。

我如何调用componentWillReceiveProps() Prop 更新后?

现在我正在通过等待实际更新的超时运行来绕过它,但我真的不喜欢这个解决方案。

  componentWillReceiveProps(){
var timeOut = setTimeout(() => this.loadPosts(), 100)
},

提前感谢您!

最佳答案

是否有必要调用componentWillReceiveProps props 更新后?或者你可以使用 nextProps argument

例如。如果你重写为:

componentWillReceiveProps(nextProps){
this.loadPosts(nextProps)
},

然后当然还要重写loadPosts的签名,允许手动传入props:

loadPosts(props = this.props){
// do something with props
...
}

关于javascript - 我如何正确使用 componentWillReceiveProps()?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41454340/

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