在React中的getDerivedStateFromProps内部调用异步函数

里雅

状态更新后,我需要更新表。因此,我尝试在getDerivedStateFromProps内部调用函数更新,但似乎无法在getDerivedStateFromProps内部使用“ this”。有什么办法吗?预先感谢。

static getDerivedStateFromProps(props, state) {
    if(props.newValue != state.newValue) {
      this.updateTable();
      return {
        newValue : props.newValue 
      }
    }
    return null;
  }
Shubham Khatri

updateTablesideEffect不需要存在功能getDerivedStateFromProps

getDerivedStateFromProps 是故意写成静态函数的,因此您不能从中调用类实例函数

为了处理sideEffect,您必须在componentDidUpdate函数中执行它们

 componentDidUpdate(prevProps, prevState) {
    if(this.props.newValue != prevProps.newValue) {
      this.updateTable();
    }
  }

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章