如何在Redux中添加多个中间件?

我已经插入了一个中间件redux-thunk,我想添加另一个redux-logger。

如何配置它,以便我的应用程序同时使用两种中间件?我尝试传递一个数组,[ReduxThunk, logger]但是没有用。

码:

import React from 'react';
import ReactDOM from 'react-dom';
import { Provider } from 'react-redux';
import { createStore, applyMiddleware } from 'redux';
import ReduxThunk from 'redux-thunk';
import logger from 'redux-logger';

import App from './components/app';
import reducers from './reducers';
require('./style.scss');

const createStoreWithMiddleware = applyMiddleware(ReduxThunk)(createStore);

ReactDOM.render(
  <Provider store={createStoreWithMiddleware(reducers)}>
    <App />
  </Provider>,
  document.querySelector('#app')
);
Gil乐2020/03/18 19:10:05

安迪的答案是好的,但是,考虑到您的中间件正在增长,下面的代码会更好:

const middlewares = [ReduxThunk, logger]
applyMiddleware(...middlewares)
GOGO2020/03/18 19:10:05

applyMiddleware将每个中间件作为新参数(而不是数组)。因此,只需传入您想要的每个中间件即可。

const createStoreWithMiddleware = applyMiddleware(ReduxThunk, logger)(createStore);