在 React 组件中,通常你不应该改变其中的 props。另外,parent 只能改变 props,不能直接改变 state。基于这两个事实,假设在 componentDidUpdate 的任何调用中是否正确,例如,
componentDidUpdate(prevProps:只读>,prevState:只读)
this.props 可能与 prevProps 不同,或者 this.state 可能与 prevState 不同,但这两种情况不能同时发生?
最佳答案
React may batch multiple setState() calls into a single update for performance.
因此我基于此做出的最佳猜测是,出于性能原因,React 可能会将 props 和状态更新组合在一起以仅执行一次更新而不是两次更新。所以回答你的问题:
this.props could be different from prevProps, OR this.state could be different from prevState, but both situations cannot occur at the same time?
我认为出于性能原因,它实际上可能会像之前所说的那样发生。
关于javascript - React componentDidUpdate 中,props 和 state 可以同时改变吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49840031/