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

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

關(guān)于redux中dispatch異步action

關(guān)于redux中dispatch異步action

有只小跳蛙 2018-08-03 20:12:40
在redux中dispatch異步action,通常是1寫法。但是不理解1和2的區(qū)別,求指導(dǎo)!const fetchPosts = (postTitle) => (dispatch, getState) => {   dispatch({ type: 'FETCH_POSTS_REQUEST' });  return fetch(`/some/API/${postTitle}.json`)     .then(response => response.json())     .then(json => dispatch(receivePosts(json)));   }; };//1store.dispatch(fetchPosts('reactjs')).then(() =>      //do sth.);//2fetchPosts('reactjs').then(() =>      //do sth.);
查看完整描述

1 回答

?
慕婉清6462132

TA貢獻1804條經(jīng)驗 獲得超2個贊

2的寫法有誤,因為fetchPost('reactjs')的返回值并不是一個promise
1為什么可以這樣寫?
以使用redux-thunk為例,封裝后的dispatch方法其實是下面的功能(完整代碼請參見github)

function (action) {    if (typeof action === 'function') {      return action(dispatch, getState, extraArgument);
    }    
    return next(action);
};

所以store.dispatch(fetch('reactjs'))拆成兩步來看
第一步:fetch('reactjs')返回的是下面的函數(shù)

(dispatch, getState) => {
  dispatch({ type: 'FETCH_POSTS_REQUEST' });  return fetch(`/some/API/${postTitle}.json`)
    .then(response => response.json())
    .then(json => dispatch(receivePosts(json)));
  };
};

第二步:調(diào)用第一步返回的函數(shù)并傳入相應(yīng)的參數(shù)


查看完整回答
反對 回復(fù) 2018-08-05
  • 1 回答
  • 0 關(guān)注
  • 3246 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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