setState
将导致re-render
组件。 对于每个render
组件,应调用getDerivedStateFromProps
。 getDerivedStateFromProps
工作的。
您应该考虑的是如何返回部分状态以进行合并。
没有示例代码,我不明白您在说什么或问什么。
预计getDerivedStateFromProps
将在setState
之后被调用,因为getDerivedStateFromProps
返回的状态基于当前props
和先前的state
。 因此,如果状态已更改(通过setState
调用),则派生状态也可能已更改(因为它是props
和state
) 。
如果props
不变,我们以前没有再调用它,但是这种行为引起了问题,因此我们在16.4中对其进行了更改(修复),如本博客文章所述:
https://reactjs.org/blog/2018/05/23/react-v-16-4.html#bugfix -for-getderivedstatefromprops
如果您想共享一些代码并询问有关为什么运行该方法的特定问题,我们中的一个可以看看。 现在,尽管我假设您的问题已经回答了:smile:
setState
将导致re-render
组件。 对于每个render
组件,应调用getDerivedStateFromProps
。getDerivedStateFromProps
工作的。您应该考虑的是如何返回部分状态以进行合并。
谢谢
@bvaughn ,谢谢你的回答