80

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')
);

7 Answers 7

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);
Sign up to request clarification or add additional context in comments.

2 Comments

That was simple enough. Thank you!
and how do you pass the reducer to the createStore method?
32

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

const middlewares = [ReduxThunk, logger]
applyMiddleware(...middlewares)

2 Comments

Thank you, but i add redux-thunk const store = createStore(reducer, composeWithDevTools(applyMiddleware(thunk, ...middleware)));
Initial post stated that array was not working with redux_thunk...same issue for me
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')
);

3 Comments

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-devtools
Probably you are not importing createStore correctly or using an old version of redux.
the problem was that one of the middleware didn't need applyMiddleware, so I had to use compose(applyMiddleware(saga), devtools()). Thanks!
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 !

1 Comment

very complete answer!
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))
 );

Comments

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.

Comments

0
export default configureStore({
  reducer: {
    products: productReducer,
    cart: cartReducer,
    [productsApi.reducerPath]: productsApi.reducer,
    userAuth: authSlice,
  },

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

});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.