在我的应用程序中,我在很多地方使用了componentWillReceiveProps。现在,我必须用getDerivedStateFromProps()或componentDidUpdate()替换它们。首先,我考虑使用getDerivedStateFromProps作为componentWillReceiveProps的替代方案,正如react-native文档所建议的那样。但有些人强烈建议不要使用这种方法,而是建议使用componentDidUpdate。但对于我的要求,所有新的道具都必须在渲染前使用state设置。getDerivedStateFromProps是最好的地方。
因此,在getDerivedStateFromProps和componentDidUpdate之间使用哪一个
发布于 2020-08-11 13:29:26
来自React文档
https://reactjs.org/docs/react-component.html#static-getderivedstatefromprops
getDerivedStateFromProps在调用render方法之前调用,无论是在初始装载时还是在后续更新时都是如此。它应该返回一个对象来更新状态,或者返回null来不更新任何内容。派生状态会导致冗长的代码,并使您的组件难以考虑。
确保您熟悉更简单的替代方案:
componentDidUpdate生命周期。https://stackoverflow.com/questions/63351774
复制相似问题