为什么 thunk 的参数包含在对象内部?

Why are the arguments to a thunk wrapped inside of an object?

我正在尝试理解 redux 中的 thunk:

const thunk = ({ dispatch, getState }) => next => action => {
  if (typeof action === 'function') {
    return action(dispatch);
  }
  return next(action);
};

而且我无法理解为什么将参数作为对象传递(或者是对象解构?)。为什么不将它们作为 dispatch, getState 传递?

Redux 中间件被赋予了 Redux 存储的微型版本 API 作为最外层函数的参数。完整商店 API 是 {dispatch, subscribe, getState, replaceReducer}。中间件只能访问 {dispatch, getState}.

至于为什么它是一个对象而不是单独的参数,that's simply an implementation detail:

    const middlewareAPI = {
      getState: store.getState,
      dispatch: (...args) => dispatch(...args)
    }
    const chain = middlewares.map(middleware => middleware(middlewareAPI))
    dispatch = compose(...chain)(store.dispatch)