大話西游666
2019-03-13 13:15:52
我有個組件是通過state更新ui的,但是它的數(shù)據(jù)需要異步請求獲取,我在哪去發(fā)送這個請求比較好?reducer創(chuàng)建的時候?異步請求貌似不行吧redux的容器層?(connect)如果放在這我如何在或獲取數(shù)據(jù)后更新state?組件的生命周期?componentWillMount不行,它只執(zhí)行一次,下次想更新沒法componentWillUpdate我當前做法是放在這,但是容易造成死循環(huán)
3 回答

長風秋雁
TA貢獻1757條經(jīng)驗 獲得超7個贊
componentWillReceiveProps(nextProps) {
if (nextProps.someReducer !== this.props.someReducer) {
this.setState({
a: reducer.a,
}, () => {
dispatch(someAction());
});
}
}
如果初始化時也需要的話,那么需要提供一個而外的函數(shù),并且在componentDidMount和componentWillReceiveProps同時處理
someLogicMethod(props) {
// todo
}
componentWillReceiveProps(nextProps) {
if (nextProps.someReducer !== this.props.someReducer) {
this.someLogicMethod(nextProps);
}
}
componentDidMount() {
this.someLogicMethod(this.props);
}

四季花海
TA貢獻1811條經(jīng)驗 獲得超5個贊
添加回答
舉報
0/150
提交
取消