• <abbr id="chdyf"></abbr>
    <ruby id="chdyf"><acronym id="chdyf"><meter id="chdyf"></meter></acronym></ruby>
    <bdo id="chdyf"></bdo>
    <dfn id="chdyf"><menu id="chdyf"></menu></dfn>
    1. <menuitem id="chdyf"></menuitem><strong id="chdyf"><menu id="chdyf"></menu></strong>

      <rt id="chdyf"><menu id="chdyf"></menu></rt>
      成人小说一区二区三区,伊人精品成人久久综合全集观看,久久HEZYO色综合,中文字幕精品人妻熟女,影音先锋成人网站,我要看免费一级毛片,中国女人做爰A片,中文字幕av久久爽Av

      理解 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.wtxcl.cn

      存檔