redux 中间件的原理是什么?

改装 dispatch
redux-thunk redux-promise redux-saga
派发action -- store -- reducer -- store
中间件 指 action 和 store 之间,dispatch是沟通的桥梁
action通常为一个对象,当我们需要传递一个函数时,需要使用中间件

https://github.com/reduxjs/redux-thunk
redux-thunk 解读
function createThunkMiddleware(extraArgument) {
  return ({ dispatch, getState }) => (next) => (action) => {
    if (typeof action === 'function') {
      return action(dispatch, getState, extraArgument);
    }

    return next(action);
  };
}

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

export default thunk;

.

原文地址:https://www.cnblogs.com/crazycode2/p/12094266.html