How to add multiple middleware to Redux?
ReactjsReduxReact ReduxMiddlewareRedux ThunkReactjs Problem Overview
I have one piece of middleware already plugged in, redux-thunk, and I'd like to add another, redux-logger.
How do I configure it so my app uses both pieces of middleware? I tried passing in an array of [ReduxThunk, logger]
but that didn't work.
Code:
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')
);
Reactjs Solutions
Solution 1 - Reactjs
applyMiddleware takes each piece of middleware as a new argument (not an array). So just pass in each piece of middleware you'd like.
const createStoreWithMiddleware = applyMiddleware(ReduxThunk, logger)(createStore);
Solution 2 - Reactjs
answer of andy's is good, but, consider your middlewares growing, below codes will be better:
const middlewares = [ReduxThunk, logger]
applyMiddleware(...middlewares)
Solution 3 - Reactjs
applyMiddleware
should pass into createStore as the second parameter. applyMiddleware
can have multiple middlewares as arguments.
const store = createStore(reducers, applyMiddleware(ReduxThunk, logger));
ReactDOM.render(
<Provider store={store}>
<App />
</Provider>,
document.querySelector('#app')
);
Solution 4 - Reactjs
This is how to apply one or many middlewares :
import {createStore, applyMiddleware} from 'redux';
import thunk from 'redux-thunk';
import logger from 'redux-logger';
import {rootReducer} from "../reducers"; // Import your combined reducers ;)
const middleWares = [thunk, logger]; // Put the list of third part plugins in an array
// Create the store with the applied middleWares and export it
export const store = createStore(rootReducer, applyMiddleware(...middleWares));
Now import the store exported recently in your index.js and pass it to the Provider component. Your index.js file should looks like :
... ...
import {Provider} from 'react-redux';
import {store} from './store';
ReactDOM.render(
<Provider store={store}>
<App />
</Provider>,
document.getElementById('root'));
That's all !
Solution 5 - Reactjs
it may be a bit late of an answer but my problem was adding the devtools with the middlewares, hope this helps somebody
// imports..
import { composeWithDevTools } from "redux-devtools-extension";
const store = createStore(
Reducer,
persistedState,
composeWithDevTools(applyMiddleware(ReduxThunk, promiseMiddleware))
);
Solution 6 - Reactjs
You can simply pass the middlewares in the comma separated manner like the following code:
const store = createStore(reducer, applyMiddleware(thunk, logger));
Note: Please import the applyMiddlware, thunk, and logger at the top.