【问题标题】:redux-thunk middleware multiple => Syntexredux-thunk 中间件 multiple => Syntex
【发布时间】: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


    【解决方案1】:

    redux-thunk 只是一个普通的 redux 中间件。中间件系统中存在某种类型的控制流。请检查here,你就会知道这是怎么回事。

    【讨论】:

      猜你喜欢
      • 2019-01-24
      • 1970-01-01
      • 1970-01-01
      • 2020-09-21
      • 2017-08-23
      • 2018-10-14
      • 2016-10-18
      • 2019-04-20
      • 2021-12-08
      相关资源
      最近更新 更多