<address id="ttjl9"></address>

      <noframes id="ttjl9"><address id="ttjl9"><nobr id="ttjl9"></nobr></address>
      <form id="ttjl9"></form>
        <em id="ttjl9"><span id="ttjl9"></span></em>
        <address id="ttjl9"></address>

          <noframes id="ttjl9"><form id="ttjl9"></form>

          理解 redux-thunk 源碼

          2020-6-5    seo達人

          前言

          前面幾篇我們就 Redux 展開了幾篇文章,這次我們來實現 react-thunk,就不是叫實現 redux-thunk 了,直接上源碼,因為源碼就11行。如果對 Redux 中間件還不理解的,可以看我寫的 Redux 文章。


          實現一個迷你Redux(基礎版)

          實現一個Redux(完善版)

          淺談React的Context API

          帶你實現 react-redux

          為什么要用 redux-thunk

          在使用 Redux 過程,通過 dispatch 方法派發一個 action 對象。當我們使用 redux-thunk 后,可以 dispatch 一個 function。redux-thunk會自動調用這個 function,并且傳遞 dispatch, getState 方法作為參數。這樣一來,我們就能在這個 function 里面處理異步邏輯,處理復雜邏輯,這是原來 Redux 做不到的,因為原來就只能 dispatch 一個簡單對象。


          用法

          redux-thunk 作為 redux 的中間件,主要用來處理異步請求,比如:


          export function fetchData() {

           return (dispatch, getState) => {

             // to do ...

             axios.get('https://jsonplaceholder.typicode.com/todos/1').then(res => {

               console.log(res)

             })

           }

          }

          redux-thunk 源碼

          redux-thunk 的源碼比較簡潔,實際就11行。前幾篇我們說到 redux 的中間件形式,

          本質上是對 store.dispatch 方法進行了增強改造,基本是類似這種形式:


          const middleware = (store) => next => action => {}

          在這里就不詳細解釋了,可以看 實現一個Redux(完善版)


          先給個縮水版的實現:


          const thunk = ({ getState, dispatch }) => next => action => {

             if (typeof action === 'function') {

                 return action(dispatch, getState)

             }

             return next(action)

          }

          export default thunk

          原理:即當 action 為 function 的時候,就調用這個 function (傳入 dispatch, getState)并返回;如果不是,就直接傳給下一個中間件。

          完整源碼如下:


          function createThunkMiddleware(extraArgument) {

           return ({ dispatch, getState }) => next => action => {

             // 如果action是一個function,就返回action(dispatch, getState, extraArgument),否則返回next(action)。

             if (typeof action === 'function') {

               return action(dispatch, getState, extraArgument)

             }

             // next為之前傳入的store.dispatch,即改寫前的dispatch

             return next(action)

           }

          }


          const thunk = createThunkMiddleware()

          // 給thunk設置一個變量withExtraArgument,并且將createThunkMiddleware整個函數賦給它

          thunk.withExtraArgument = createThunkMiddleware


          export default thunk

          我們發現其實還多了 extraArgument 傳入,這個是自定義參數,如下用法:


          const api = "https://jsonplaceholder.typicode.com/todos/1";

          const whatever = 10;


          const store = createStore(

           reducer,

           applyMiddleware(thunk.withExtraArgument({ api, whatever })),

          );


          // later

          function fetchData() {

           return (dispatch, getState, { api, whatever }) => {

             // you can use api and something else here

           };

          }

          總結

          同 redux-thunk 非常流行的庫 redux-saga 一樣,都是在 redux 中做異步請求等副作用。Redux 相關的系列文章就暫時寫到這部分為止,下次會寫其他系列。

          日歷

          鏈接

          個人資料

          藍藍設計的小編 http://www.syprn.cn

          存檔

          亚洲va欧美va天堂v国产综合