在组件安装上调用几个(多个)异步动作是否可以?

时间:2018-12-18 17:37:33

标签: reactjs asynchronous redux action

可以使用componentDidMount方法调度几个异步操作吗? 其中一些已连接-例如,首先我获取属于实际用户的报告类型,然后根据报告类型获取报告详细信息。现在,我要调用另一个“报表详细信息”操作。就像:

componentDidMount() {
  this.props.reportTypesAction().then(
    () => {
      this.setState({ reportTypes: this.props.reportTypes });
      this.props.reportAction();
      this.propt.anotherReportAction();
    }
  )
}

1 个答案:

答案 0 :(得分:0)

如果应该在componentDidMount中调用任意数量的函数,则可以。但是,处理未立即提供给回调但已保存到redux存储并从promise链中的props引用的promise不是最佳的。

我的意思是,您可以同步地调度单个reportTypesAction来存储和使用redux-saga来轻松处理涉及承诺的其余更新。

this.setStatecomponentDidUpdate中有条件地调用使用单个分发方法shouldComponentUpdate,因为在componentDidMount中,this.props.reportTypes可能为空。

这也可以避免不必要的重新渲染。