react生命周期函数componentWillReceiveProps(通过比较先后传递的props刷新组件)

componentWillReceiveProps这个生命周期函数用的不多,碰到正好总结下。
该函数通过比较先后两次传入的props数值是否相同,执行相应的方法。

如代码所示,比较前一次传入的type, historyData和现在的是否相同,决定是否如何填充echarts options

  componentWillReceiveProps(nextProps) {
    
    
    const {
    
     type, historyData } = nextProps;
    // eslint-disable-next-line react/destructuring-assignment
    if (this.props.historyData !== historyData || this.props.type !== type) {
    
    
      this.handleOption(type, historyData);
    }
  }

猜你喜欢

转载自blog.csdn.net/qq_34307801/article/details/105949631