如何将参数传递给自定义 Redux 中间件?
How to pass arguments into custom Redux middleware?
我想将参数传递给自定义 Redux 中间件。
但是我不知道如何使用自定义中间件中的参数。
示例:
import { createStore, applyMiddleware } from 'redux';
import reducers from './reducers';
const customMiddleWare = store => next => action => {
// how to use argument here?
};
const middlewares = [customMiddleWare(argument)];
const store = createStore(
reducers,
applyMiddleware(...middlewares)
);
由于中间件是函数,您可以创建一个包装函数来接收您的参数,return您的中间件函数将可以访问所提供的参数。
示例:
const middlewareWrapper = customArgument => store => next => action =>
console.log(customArgument);
}
const middlewares = [middlewareWrapper(argument)];
const store = createStore(
reducers,
applyMiddleware(...middlewares)
);
我想将参数传递给自定义 Redux 中间件。 但是我不知道如何使用自定义中间件中的参数。
示例:
import { createStore, applyMiddleware } from 'redux';
import reducers from './reducers';
const customMiddleWare = store => next => action => {
// how to use argument here?
};
const middlewares = [customMiddleWare(argument)];
const store = createStore(
reducers,
applyMiddleware(...middlewares)
);
由于中间件是函数,您可以创建一个包装函数来接收您的参数,return您的中间件函数将可以访问所提供的参数。
示例:
const middlewareWrapper = customArgument => store => next => action =>
console.log(customArgument);
}
const middlewares = [middlewareWrapper(argument)];
const store = createStore(
reducers,
applyMiddleware(...middlewares)
);