【发布时间】:2018-08-02 13:46:10
【问题描述】:
如您所知,componentWillReceiveProps 等一些生命周期方法现已弃用。
好吧,我的代码使用了这种生命周期方法,现在我不知道如何迁移到 React 的新 API。
这是我的使用方法:
componentWillReceiveProps(nextProps) {
if (nextProps.newPost) {
this.props.posts.unshift(nextProps.newPost);
}
}
在 react 博客中我可以读到...
与 componentDidUpdate 一起,这个新的生命周期应该涵盖所有 旧组件WillReceiveProps 的用例。
...但是如何实现呢?
【问题讨论】:
-
嗯,基本上 React 团队指向(如你所提到的)
componentDidMount或者可能使用这里讨论的技术之一:reactjs.org/docs/…,componentDidUpdate可以访问组件的状态一个道具直接,例如:this.props和this.state。最后,你的这部分代码引起了我的注意:this.props.posts.unshift(nextProps.newPost);你是在直接在组件的props上改变posts数组吗?对我来说,这似乎是一个no-no。
标签: reactjs react-redux