How to add multiple middleware to Redux?
Asked Answered
G

7

79

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')
);
Gahan answered 13/5, 2017 at 16:3 Comment(0)
A
126

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);
Amargo answered 13/5, 2017 at 16:6 Comment(2)
That was simple enough. Thank you!Gahan
and how do you pass the reducer to the createStore method?Mythicize
P
32

answer of andy's is good, but, consider your middlewares growing, below codes will be better:

const middlewares = [ReduxThunk, logger]
applyMiddleware(...middlewares)
Piggery answered 6/5, 2018 at 9:7 Comment(2)
Thank you, but i add redux-thunk const store = createStore(reducer, composeWithDevTools(applyMiddleware(thunk, ...middleware)));Eley
Initial post stated that array was not working with redux_thunk...same issue for meBosk
O
12

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')
);
Operand answered 13/5, 2017 at 16:11 Comment(3)
If I do this I get "createStore is not a function". Any idea where this error could be coming from? I'm trying to use redux-saga and remote-redux-devtoolsPignut
Probably you are not importing createStore correctly or using an old version of redux.Operand
the problem was that one of the middleware didn't need applyMiddleware, so I had to use compose(applyMiddleware(saga), devtools()). Thanks!Pignut
S
2

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 !

Swiger answered 7/4, 2019 at 16:50 Comment(1)
very complete answer!Cork
D
1

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))
 );
Derward answered 16/10, 2020 at 15:45 Comment(0)
N
1

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.

Nylon answered 6/1, 2022 at 12:27 Comment(0)
W
0
export default configureStore({
  reducer: {
    products: productReducer,
    cart: cartReducer,
    [productsApi.reducerPath]: productsApi.reducer,
    userAuth: authSlice,
  },

  middleware: (getDefaultMiddleware) =>
    getDefaultMiddleware({
      serializableCheck: false
    }).concat(productsApi.middleware),

});
Whoso answered 15/1, 2023 at 7:24 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.