为什么在这些方法中使用setState时,componentWillUpdate()和componentWillMount()不会触发渲染函数?

6 javascript components reactive-programming reactjs

我们可以在每个组件生命周期方法中调用setState().

为什么我们不在componentWillUpdate()和componentWillMount()中调用它?

为什么这些方法在将setState放入其中时不会触发渲染功能?

谁能详细解释一下吗?

谢谢.

Yad*_*ran 14

componentWillMount()中的setState()

componentWillMount()在安装发生之前立即调用.之前调用它render(),因此在此方法中设置状态不会触发重新渲染.

setState()in componentWillMount()不会触发重新渲染,但我们此时从未打过render()电话.因此,在此处设置状态将在我们进入render()第一遍之前准备状态对象.

实际上,对于准备态对象我们的第一个render(),更好的办法是使用constructor()调用,而不是setStatecomponentWillMount.

更多阅读:


componentWillUpdate()中的setState()

请注意,你不能this.setState()在这里打电话.如果您需要更新状态以响应支柱更改,请componentWillReceiveProps() 改用.

componentWillUpdate()每次需要重新渲染时this.setState()调用,例如调用时.我们不在this.setState()这里调用的原因是该方法触发另一个componentWillUpdate().如果我们触发状态改变,componentWillUpdate()我们将以无限循环结束.

更多阅读: