Redux Async Solution Redux-Thunk

function createThunkMiddleware(extraArgument) {
  return function thunk(store) {
    return function (next) {
      return function (action) {
        // Destructure dispatch, getState from store
        const { dispatch, getState } = store;

        // If action is function, call it with dispatch and getState
        if (typeof action === 'function') {
          return action(dispatch, getState, extraArgument);   // extraArgument can be passed here
        }

        // Otherwise treat as normal action
        let result = next(action);
        return result
      }
    }
  }
}

const thunk = createThunkMiddleware();
thunk.withExtraArgument = createThunkMiddleware;

export default thunk;
Explanation: https://segmentfault.com/a/1190000037437347

评论

暂无评论。

登录后可发表评论。