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行,嵌套和可组合性是使
中间件系统功能强大
你看过文件了吗?