ComponentWillReceiveProps正在改变我从API获得的道具

时间:2018-03-31 11:30:54

标签: reactjs react-props react-lifecycle

在我的ComponentWillReceiveProps()方法中,我的道具正在更新,仅从API获取。请帮助如何恢复道具。

我的ComponentWillReceiveProps()

constructor(props) {
  super(props)
  this.state = {
    filtered_chart_data: {}
  }
  props.getEngagementInfo()
  props.getChartData()
}


componentWillReceiveProps(nextProps) {
  const lastSavedCharts = this.state.filtered_chart_data
  if (!_.isEmpty(nextProps.diagnose.chart_data)) {
    if (_.isEmpty(this.state.filtered_chart_data)) {
      return this.setState({
        filtered_chart_data: nextProps.diagnose.chart_data
      })
    }
    return this.setState(
      {
        filtered_chart_data: lastSavedCharts
      },
      () => this.updateFilters(nextProps.diagnose.chart_data)
    )
  }
}

updateFilters = chartDataToApplyFilters => {
  if (!_.isEmpty(this.state.filtered_chart_data)) {
    const { filters } = this.props.diagnose
    this.handleFilterPlantRegion(
      filters.plant_region,
      chartDataToApplyFilters
    )
    this.handleFilterPlant(filters.plant, chartDataToApplyFilters)
  }
}

在我的nextProps中,变量nextProps.diagnose.chart_data每次都在更新,但它是从API中获取的。

你能帮忙怎么不更新这个道具?

1 个答案:

答案 0 :(得分:0)

您可以从docs

尝试shouldComponentUpdate()