https://facebook.github.io/react/docs/react-component.html#setstate
我似乎官方文档说:
The second parameter to setState() is an optional callback function that will be executed once setState is completed and the component is re-rendered. Generally we recommend using componentDidUpdate() for such logic instead.
当我们需要使用最新状态时,为什么他们建议使用 componentDidUpdate 而不是回调?
最佳答案
componentDidUpdate
也会在每次更新发生后立即调用。此时,最新状态已更新。
将所有检查状态逻辑放在 componentDidUpdate
中比每个 setState
的许多 callback
更容易管理。
顺便说一句,如果您使用 redux
,我们不会在您的组件中执行 setState
操作。
More about redux
注意:初始渲染时不会调用componentDidUpdate
。
了解更多组件生命周期 https://facebook.github.io/react/docs/react-component.html#componentdidupdate
关于reactjs - 为什么官方文档建议在 setState 中使用 componentDidMount 而不是回调?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45587645/