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

鍍金池/ 問答/HTML/ 如何讓effects異步回調(diào)之后更新組件的state?

如何讓effects異步回調(diào)之后更新組件的state?

之所以需要異步回調(diào)后更新組件的state是因為

  1. state中的某個值綁定了某個彈窗用于顯示的屬性

希望達到的效果是:
在異步回調(diào)后更新組件state,從而使彈窗組件顯示。

回答
編輯回答
萢萢糖

將這個彈出框?qū)懸粋€function component:

export default (conf) => {
  //添加一個節(jié)點供彈出框渲然
  if (document.getElementById('DialogDiv')) {
    return
  }
  let div = document.createElement('div');
  div.id = 'DialogDiv'
  document.body.appendChild(div);
  ReactDOM.render(<Dialog {...conf} div={div}/>, div);
};

Dialog 組件自己實現(xiàn)。
然后通過 new Promise來實現(xiàn)異步:

export default ({type, message, title, locale}) => {
  return new Promise(resolve => {
    ReservationDialog({type, message, title, locale, onClose: e => resolve(e)});
  });
};

ReservationDialog就是第一步export出的組件。
最后在effects中通過call來調(diào)用:

 const result = yield call(yourService);

 const data = yield call(openDialog, {message: result.message, ...result});
 if(data.Ok === 'OK') {
    //單擊了OK按鈕
 }
 if(data.CANCEL === 'CANCEL') {
    //單擊了CANCEL按鈕
 }

 //data為返回的數(shù)據(jù),即promise.then后的值。
2017年10月31日 18:30