Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/72.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Redux applyMiddleware()中的“next”是什么?_Redux_React Redux - Fatal编程技术网

Redux applyMiddleware()中的“next”是什么?

Redux applyMiddleware()中的“next”是什么?,redux,react-redux,Redux,React Redux,我正在阅读applyMiddleware的源代码,在一些代码片段中,他们这样称呼它applyMiddleware(…中间件)。那么next参数什么时候通过 export default function applyMiddleware(...middlewares) { return (next) => (reducer, initialState) => { var store = next(reducer, initialState);

我正在阅读
applyMiddleware
的源代码,在一些代码片段中,他们这样称呼它
applyMiddleware(…中间件)
。那么
next
参数什么时候通过

export default function applyMiddleware(...middlewares) {
  return (next)  =>
     (reducer, initialState) => {
        var store = next(reducer, initialState);
        var dispatch = store.dispatch;
        var chain = [];

        var middlewareAPI = {
          getState: store.getState,
          dispatch: (action) => dispatch(action)
        };

        chain = middlewares.map(middleware => middleware(middlewareAPI));

        dispatch = compose(...chain, store.dispatch);
        return {
          ...store,
          dispatch
        };
   };
}

中间件
中的每个
中间件
都具有以下通用格式:

// condensed, does not do anything, just calls the next middleware
function middleware({ getState }) => (next) => (action) => next(action)

or

// expanded, does not do anything, just calls the next middleware
function middleware({ getState }) {
  return function (next) {
    return function (action) {
      return next(action)
    }
  }
}
中间件是一个返回函数的函数,该函数返回调用下一个中间件(如果存在)的函数

为了回答您的问题,
next
作为参数传递给第二个函数。这样,
applyMiddleware
store增强器就可以调用链中的下一个中间件。中间件语法有点混乱(从以下方面):

中间件听起来比实际情况复杂得多。唯一的 真正理解中间件的方法是查看现有的 中间件可以工作,并尝试编写自己的中间件。函数嵌套可以 这很吓人,但事实上,您会发现大多数中间件, 10行,嵌套和可组合性是使 中间件系统功能强大

你看过文件了吗?