需要时未调用React componentDidUpdate

时间:2019-04-12 14:56:45

标签: reactjs redux

我有一个父母,当道具改变时,它将发送新数据请求。

我有一个时间范围选择器组件(子组件),它将默认为包装在其上的从redux状态接收到的最后8周数据。它将在“ timeRange”状态下调用调度。

然后,父母应该检测到包裹在自身上的'timeRange'道具也发生了变化,并触发请求。 (我希望如此)。

class Parent extends component {

  componentDidUpdate(prevProps) {
    const { timeRange } = this.props;
    if (timeRange !== prevProps.timeRange) {
      getData()
    }
  }

  render() {
    console.log(this.props.timeRange)
    // Logs the timeRange set by Child even though
    // INITIAL_STATE should have been applied to the Parent.

    return (<Child />)
  }
}


    class Child extends Component {


  componentDidMount() {

    const { availableDates } = this.props;
    /*
      logic to set dateRange on availableDates
     */
    setDateRange(newRange)

  }
}

请注意,availableDates是在父项挂载之前很早就由一个更高阶的组件检索的。

任何有关如何改进我的流程的建议都将受到赞赏。

0 个答案:

没有答案