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;
1、redux-thunk的源码就这几段代码,正常的我们dispatch(action), 我们需要action的数据格式是一个对象类型,如果我们用了redux-thunk中间件,我们的action就可以是一个function类型。
2、我们发现,我们上面的代码如果action是一个function类型,就不会执行next函数了,那么后面的中间件要如何执行呢?我们一般在使用thunk中间件时,会在action函数中,再去执行一次dispatch(action),这次执行的action会是一个对象类型,就会通过中间件模型一直往下执行
