当有上下文参数时,如何替换componentWillReceiveProps?

问题描述 投票:2回答:1

在反应中,替换componentWillReceiveProps的标准方法是将getDerivedStateFromPropsgetSnapshotBeforeUpdatecomponentDidUpdate一起使用。当componentWillReceiveProps具有第二个参数上下文并且您需要其中的属性时,该如何替换?

例如,假设代码是这样的-

componentWillReceiveProps(nextProps, context) {
     if (context.href !== this.context.href) {
          const parsedUrl = url.parse(context.href, true);
          const from = parseInt(parsedUrl.query.from, 100) || 0;
          this.setState({
            from,
            more: [],
           });
     }
}

大多数在线示例显示了从componentWillReceiveProps的迁移getDerivedStateFromPropgetSnapshotBeforeUpdate的参数没有参数componentWillReceiveProps' context参数。 getDerivedStateFromPropgetSnapshotBeforeUpdate都没有nextContext。那么,context在迁移过程中如何处理?

javascript reactjs upgrade react-component
1个回答
0
投票

我仍在调查中。到目前为止,答案似乎是迁移到获取上下文的下一个上下文API方法-https://reactjs.org/docs/context.html

© www.soinside.com 2019 - 2024. All rights reserved.