reactjs - 比较 componentDidUpdate 中的 PrevProps

标签 reactjs react-props

我试图检测已安装组件的 componentDidUpdate 内的 prop 何时发生更改。我有一个测试(下面代码中的refreshData)运行良好。是否有可能以 componentDidUpdate(prevProps) 无法检测到的方式传递 props?

在组件.js 中:

componentDidUpdate(prevProps){

    //works fine
    if ( this.props.refreshData !== prevProps.refreshData ) {
        if ( this.props.refreshData )
            this.refreshData();
    }

    //these two arent calling
    if ( this.props.selectedCountries !== prevProps.selectedCountries ) {
        if ( this.props.selectedCountries )
            console.log('updated selected countries');
    }

    if ( this.props.selectedLocations !== prevProps.selectedLocations ) {
        console.log('updated selected locations');
    }

}

并在 App.js 中传递如下属性:

selectLocation = (id, type, lng, lat, polydata, name, clear = false) => {

  //console.log(type);
  //console.log(lng);
  //console.log(lat);
  //console.log(polydata);

  let selectedType = 'selected' + type;
  let previousState = [];

  if (clear) {
    this.setState({
      selectedCountries: [],
      selectedLocations: [],
      selectedServices: [],
      selectedPoints: [],
      mapCenter: [lng, lat],
      locationGeoCoords: [polydata]
    })
    previousState.push(id);

  } else {

    previousState = this.state[selectedType];
    if (previousState.indexOf(id) === -1) {
      //push id
      previousState.push(id);

    } else {
      //remove id
      var index = previousState.indexOf(id)
      previousState.splice(index, 1);
    }
  }

  if (type === "Countries") {

    this.setState({
      selectedCountries: previousState,
      refreshData: true,
    })
  } else if (type === "Locations") {
    this.setState({
      selectedLocations: previousState,
      refreshData: true
    })
  } else if (type === "Points") {
    this.setState({
      selectedPoints: previousState,
      refreshData: true
    })
  }


}


render() {
  return (

    <component
      selectedCountries={this.state.selectedCountries}
      selectedLocations={this.state.selectedLocations}
      refreshData={this.state.refreshData} />
  }
}

最佳答案

嗨:)正如我的评论中所述,问题出在您的 App.js 文件中 - 您正在改变一个数组。换句话说,当您认为正在创建要传递的选定国家/地区的新数组时,您实际上是在更新原始数组,因此当您进行比较时,您总是在比较两个完全相同的数组。

尝试像这样更新你的 App.js -

selectLocation = (id, type, lng, lat, polydata, name, clear = false) => {

  //console.log(type);
  //console.log(lng);
  //console.log(lat);
  //console.log(polydata);

  let selectedType = 'selected' + type;
  let previousState = [];

  if (clear) {
    this.setState({
      selectedCountries: [],
      selectedLocations: [],
      selectedServices: [],
      selectedPoints: [],
      mapCenter: [lng, lat],
      locationGeoCoords: [polydata]
    })
    previousState.push(id);

  } else {

    previousState = [].concat(this.state[selectedType]);
    if (previousState.indexOf(id) === -1) {
      //push id
      previousState.push(id);

    } else {
      //remove id
      var index = previousState.indexOf(id)
      previousState.splice(index, 1);
    }
  }

  if (type === "Countries") {

    this.setState({
      selectedCountries: previousState,
      refreshData: true,
    })
  } else if (type === "Locations") {
    this.setState({
      selectedLocations: previousState,
      refreshData: true
    })
  } else if (type === "Points") {
    this.setState({
      selectedPoints: previousState,
      refreshData: true
    })
  }


}


render() {
  return (

    <component
      selectedCountries={this.state.selectedCountries}
      selectedLocations={this.state.selectedLocations}
      refreshData={this.state.refreshData} />
  }
}

唯一的区别是您设置 previousState 的行 - 我将其更新为

previousState = [].concat(this.state[selectedType]);

通过添加 [].concat 我每次都有效地创建一个新数组,因此当您通过 push/ 将更改应用到数组时splice 您将仅修改新数组。然后,一旦您将其作为 props 传递下去,比较就会正常进行:)

为了满足您的阅读兴趣,我找到了一篇对此进行了一些讨论的帖子:https://medium.com/pro-react/a-brief-talk-about-immutability-and-react-s-helpers-70919ab8ae7c

关于reactjs - 比较 componentDidUpdate 中的 PrevProps,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52393172/

相关文章:

javascript - React 安装了 React Router 但无法让页面呈现

node.js - 为什么当我运行 Angular/React 项目时它无法在我的本地主机上运行?

javascript - 类型 'Element | undefined' 不可分配给类型 'ReactElement<any, string | (( Prop : any)

javascript - 空对象从无处传递到 Prop 上

reactjs - 在 TypeScript 中为 React 组件进行继承的正确方法是什么?

javascript - 当用户输入时,如何在react-native TextInput中每3位数字添加点?

javascript - 为什么 react 路由器抛出 'Cannot GET/example' 错误?

javascript - 有条件的 onclick 函数在没有点击的情况下触发?

reactjs - 将状态从子组件传递到父组件

javascript - 在 React 构造函数中访问 props