可以使用componentDidMount
方法调度几个异步操作吗?
其中一些已连接-例如,首先我获取属于实际用户的报告类型,然后根据报告类型获取报告详细信息。现在,我要调用另一个“报表详细信息”操作。就像:
componentDidMount() {
this.props.reportTypesAction().then(
() => {
this.setState({ reportTypes: this.props.reportTypes });
this.props.reportAction();
this.propt.anotherReportAction();
}
)
}
答案 0 :(得分:0)
如果应该在componentDidMount
中调用任意数量的函数,则可以。但是,处理未立即提供给回调但已保存到redux存储并从promise链中的props引用的promise不是最佳的。
我的意思是,您可以同步地调度单个reportTypesAction
来存储和使用redux-saga
来轻松处理涉及承诺的其余更新。
在this.setState
或componentDidUpdate
中有条件地调用使用单个分发方法shouldComponentUpdate
,因为在componentDidMount
中,this.props.reportTypes
可能为空。
这也可以避免不必要的重新渲染。