在线观看不卡亚洲电影_亚洲妓女99综合网_91青青青亚洲娱乐在线观看_日韩无码高清综合久久

鍍金池/ 問(wèn)答/HTML/ react和reudx ,我應(yīng)該在哪初始化組件的數(shù)據(jù)?

react和reudx ,我應(yīng)該在哪初始化組件的數(shù)據(jù)?

我有個(gè)組件是通過(guò)state更新ui的,但是它的數(shù)據(jù)需要異步請(qǐng)求獲取,我在哪去發(fā)送這個(gè)請(qǐng)求比較好?

  1. reducer創(chuàng)建的時(shí)候?
    異步請(qǐng)求貌似不行吧
  2. redux的容器層?(connect)
    如果放在這我如何在或獲取數(shù)據(jù)后更新state?
  3. 組件的生命周期?
  • componentWillMount
    不行,它只執(zhí)行一次,下次想更新沒(méi)法
  • componentWillUpdate
    我當(dāng)前做法是放在這,但是容易造成死循環(huán)
回答
編輯回答
你的瞳

“不行,它只執(zhí)行一次,下次想更新沒(méi)法”,你是怎么更新的?用戶(hù)操作點(diǎn)擊更新?還是自動(dòng)每幾秒更新?

2018年8月9日 10:30
編輯回答
柒喵

在componentDidMount里異步請(qǐng)求獲取,然后把數(shù)據(jù)存到redux里,state應(yīng)該是綁的redux的值,然后redux更新的時(shí)候,state也會(huì)實(shí)時(shí)更新的。

2018年3月22日 05:57
編輯回答
淺時(shí)光
componentWillReceiveProps(nextProps) {
  if (nextProps.someReducer !== this.props.someReducer) {
    this.setState({
      a: reducer.a,
    }, () => {
       dispatch(someAction());
    });
  }
}

如果初始化時(shí)也需要的話(huà),那么需要提供一個(gè)而外的函數(shù),并且在componentDidMount和componentWillReceiveProps同時(shí)處理

someLogicMethod(props) {
  // todo
}

componentWillReceiveProps(nextProps) {
  if (nextProps.someReducer !== this.props.someReducer) {
    this.someLogicMethod(nextProps);
  }
}

componentDidMount() {
  this.someLogicMethod(this.props);
}
2018年7月10日 15:45