Redux和shouldComponentUpdate

时间:2018-10-25 21:03:21

标签: reactjs redux lifecycle

如果props中数组的长度与前一个不匹配,我试图使我的组件之一更新。问题是我对componentWillMount进行了redux操作。因此,当我将this.props.array传递给shouldComponentUpdate中的函数时,它最初是空的,因此它无法按我的要求工作。

我想知道是否有人对此有建议或知道更好的方法/解决方案?预先感谢。

     componentDidMount() {
    this.props.getAllCourses(this.props.user.id);
  }

  shouldComponentUpdate(nextProps) {
    return compareCoursesProps(nextProps, this.props.courses);
  }

  render() {

1 个答案:

答案 0 :(得分:0)

您可以为其使用getDerivedStateFromProps生命周期。这可能会让您有个主意

    state = { yourArray:[] }

static getDerivedStateFromProps(nextProps, prevState)      {
if (nextProps.yourArray !== prevState.yourArray) {
    return { yourArray: nextProps.yourArray};
}
return null;
}
相关问题