我有个组件是通过state更新ui的,但是它的数据需要异步请求获取,我在哪去发送这个请求比较好?reducer创建的时候?异步请求貌似不行吧redux的容器层?(connect)如果放在这我如何在或获取数据后更新state?组件的生命周期?componentWillMount不行,它只执行一次,下次想更新没法componentWillUpdate我当前做法是放在这,但是容易造成死循环
3 回答

长风秋雁
TA贡献1757条经验 获得超7个赞
componentWillReceiveProps(nextProps) {
if (nextProps.someReducer !== this.props.someReducer) {
this.setState({
a: reducer.a,
}, () => {
dispatch(someAction());
});
}
}
如果初始化时也需要的话,那么需要提供一个而外的函数,并且在componentDidMount和componentWillReceiveProps同时处理
someLogicMethod(props) {
// todo
}
componentWillReceiveProps(nextProps) {
if (nextProps.someReducer !== this.props.someReducer) {
this.someLogicMethod(nextProps);
}
}
componentDidMount() {
this.someLogicMethod(this.props);
}

四季花海
TA贡献1811条经验 获得超5个赞
添加回答
举报
0/150
提交
取消