在 getDerivedStateFromProps 中调用一个方法

Call a method in getDerivedStateFromProps

目前我的代码是这样的:

componentWillReceiveProps(nextProps: any) {
  this.showLockedWarning(nextProps);
}

但由于 componentWillReceiveProps 已被弃用,我想使用没有此类问题的函数,据我所知,我应该有 2 个选项 - getDerivedStateFromPropscomponentDidUpdate.

我无法使用 componentDidUpdate,因为这会造成无限循环,但从 getDerivedStateFromProps 我无法访问 this 关键字。在这种情况下我能做什么?

如果您不更改状态(如示例中所示),那么最好将代码移至 componentDidUpdate(),但如果您需要更改状态 调用一个函数,那么你也可以创建你想调用的函数static