如何确定替换 componentWillReceiveProps 并不断获取 nextProps?

Sté*_*lis 3 reactjs react-redux

我在我的应用程序中运行 React 16.8.4,在某些情况下我确实需要使用 nextProps。正如 React 文档提到的,componentWillReceiveProps 是一种不安全的方法,不建议再使用它。

我阅读了文档(https://reactjs.org/blog/2018/06/07/you-probously-dont-need-driven-state.html#recommendation-filled-uncontrol-component-with-a-key)但是我什至不知道如何使用此版本的密钥和所有内容来移植我的旧 componentWillReceiveProps。

我不需要创建新的组件实例,我只需要 nextProps!!!我尝试使用 componentDidUpdate(),但我不想获得 prevProps,我想要 NEXT PROPS!

你们能帮忙吗?

Tho*_*lle 6

this.propsin将与incomponentDidUpdate相同,因此您可以使用它。nextPropscomponentWillReceiveProps

// componentWillReceiveProps(nextProps) {
//   if (nextProps.someProp !== this.props.someProp) {
//     doSomething();
//   }
// }

componentDidUpdate(prevProps) {
  if (this.props.someProp !== prevProps.someProp) {
    doSomething();
  }
}
Run Code Online (Sandbox Code Playgroud)