如何将参数传递给Redux中间件?

时间:2019-03-26 14:29:56

标签: javascript reactjs redux react-redux 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)
);

1 个答案:

答案 0 :(得分:2)

由于中间件是函数,因此您可以创建一个包装函数,该包装函数将接受您的参数,并返回可以访问所提供参数的中间件函数。

示例:

const middlewareWrapper = customArgument => store => next => action =>
  console.log(customArgument);
}

const middlewares = [middlewareWrapper(argument)];

const store = createStore(
  reducers,
  applyMiddleware(...middlewares)
);