开发者社区 问答 正文

#React getDerivedStateFromProps()生命周期方法的目的是什么?

#React getDerivedStateFromProps()生命周期方法的目的是什么?

展开
收起
因为相信,所以看见。 2020-05-07 17:58:58 1070 分享 版权
1 条回答
写回答
取消 提交回答
  • 阿里,我所有的向往

    在getDerivedStateFromProps()实例化组件之后以及重新呈现组件之前,将调用新的静态生命周期方法。它可以返回一个对象以更新状态,或null指示新的道具不需要任何状态更新。

    class MyComponent extends React.Component {
      static getDerivedStateFromProps(props, state) {
        // ...
      }
    }
    
    

    该生命周期方法以及componentDidUpdate()涵盖了所有的用例componentWillReceiveProps()。

    2020-05-07 18:04:29
    赞同 展开评论