在反应中,替换componentWillReceiveProps
的标准方法是将getDerivedStateFromProps
或getSnapshotBeforeUpdate
与componentDidUpdate
一起使用。当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
的迁移getDerivedStateFromProp
或getSnapshotBeforeUpdate
的参数没有参数componentWillReceiveProps'
context
参数。 getDerivedStateFromProp
和getSnapshotBeforeUpdate
都没有nextContext
。那么,context
在迁移过程中如何处理?
我仍在调查中。到目前为止,答案似乎是迁移到获取上下文的下一个上下文API方法-https://reactjs.org/docs/context.html