【问题标题】:ComponentWillReceiveProps are changing my props which I was getting from APIComponentWillReceiveProps 正在更改我从 API 获得的道具
【发布时间】:2018-03-31 11:30:54
【问题描述】:

在我的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 获取的。

你能帮忙如何不更新这个道具吗?

【问题讨论】:

    标签: reactjs react-props react-lifecycle


    【解决方案1】:

    你可以试试 shouldComponentUpdate(),来自docs

    【讨论】:

    • 好的,谢谢
    猜你喜欢
    • 2018-07-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-03-20
    相关资源
    最近更新 更多