第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

返回一個(gè)簡(jiǎn)單動(dòng)作創(chuàng)建者的承諾,以使用react-redux做出反應(yīng)

返回一個(gè)簡(jiǎn)單動(dòng)作創(chuàng)建者的承諾,以使用react-redux做出反應(yīng)

藍(lán)山帝景 2021-04-03 19:15:00
我是react-redux的新手。在這里,我認(rèn)為這是一個(gè)非?;镜膯栴}。但是,我有一個(gè)動(dòng)作創(chuàng)建者,并且我在使用redux thunk。export const updateActivePage = (activePage) => {  return {    type: UPDATE_ACTIVEPAGE,    payload: activePage  }}我試過的是export const updateActivePage = (activePage) => (dispatch) => {  return new Promise( function(resolve, reject) {    dispatch({      type: UPDATE_ACTIVEPAGE,      payload: activePage    })  })}之后的函數(shù)不會(huì)被調(diào)用?,F(xiàn)在,在我componentdidmount要.then在此之后使用因此,我要為此答應(yīng)。那么,我該怎么做呢?我正在使用reux-thunk
查看完整描述

2 回答

?
桃花長(zhǎng)相依

TA貢獻(xiàn)1860條經(jīng)驗(yàn) 獲得超8個(gè)贊

直接來自redux-thunk自述文件:


function makeASandwichWithSecretSauce(forPerson) {

  // We can invert control here by returning a function - the "thunk".

  // When this function is passed to `dispatch`, the thunk middleware will intercept it,

  // and call it with `dispatch` and `getState` as arguments. 

  // This gives the thunk function the ability to run some logic, and still interact with the store.

  return function (dispatch) {

    return fetchSecretSauce().then(

      sauce => dispatch(makeASandwich(forPerson, sauce)),

      error => dispatch(apologize('The Sandwich Shop', forPerson, error))

    );

  };

}


...


// It even takes care to return the thunk’s return value

// from the dispatch, so I can chain Promises as long as I return them.


store.dispatch(

  makeASandwichWithSecretSauce('My partner')

).then(() => {

  console.log('Done!');

});


查看完整回答
反對(duì) 回復(fù) 2021-04-15
?
Qyouu

TA貢獻(xiàn)1786條經(jīng)驗(yàn) 獲得超11個(gè)贊

當(dāng)您想從外部源(例如REST)中獲取某些數(shù)據(jù)時(shí),會(huì)使用Promise。但是,如果您確實(shí)要執(zhí)行此操作,則動(dòng)作創(chuàng)建者必須返回一個(gè)函數(shù):


export const updateActivePage = (activePage) => {

  return (dispatch) => {

    return dispatch ({

      type: UPDATE_ACTIVEPAGE,

      payload: activePage

    });

  }

}


這樣的事情應(yīng)該返回承諾。但是,將在分派操作時(shí)解決此承諾。它與redux狀態(tài)更改不同。我認(rèn)為您真的不想在這里使用promise。


如果要在redux狀態(tài)發(fā)生變化時(shí)做出反應(yīng),則組件應(yīng)觀察狀態(tài)(使用mapStateToProps),并且可以處理componentWillUpdate方法中的更改。


查看完整回答
反對(duì) 回復(fù) 2021-04-15
  • 2 回答
  • 0 關(guān)注
  • 192 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)