【发布时间】:2019-03-15 01:29:19
【问题描述】:
我试图了解 thunk 在 shell 下的工作原理,但很难理解从 here 引用的这段代码
function createThunkMiddleware(extraArgument) {
return ({ dispatch, getState }) => next => action => {
// This gets called for every action you dispatch.
// If it's a function, call it.
if (typeof action === 'function') {
return action(dispatch, getState, extraArgument);
}
// Otherwise, just continue processing this action as usual
return next(action);
};
}
const thunk = createThunkMiddleware();
thunk.withExtraArgument = createThunkMiddleware;
export default thunk;
它说这基本上是 thunk 的代码。这里让我感到困惑的是多重 => 语法。我知道 => 是箭头函数的一部分。
我目前的理解是createThunkMiddleware返回一个函数,我们称它为A,它返回另一个函数B,它返回另一个函数C。A、B和C的签名如下:
function A ({dispatch, getState}) {
return B(next)
}
function B (next) {
return C(action)
}
function C (action) {
....
}
但这对我来说没有意义。因为,A 如何将next 传递给 B,B 也是如此。
【问题讨论】:
标签: javascript redux redux-thunk