Source:  Twitter logo

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

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);
120 users liked answer #0dislike answer #0120
Andy Noelker profile pic
Andy Noelker

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

const middlewares = [ReduxThunk, logger]
applyMiddleware(...middlewares)
31 users liked answer #1dislike answer #131
chen Jacky profile pic
chen Jacky

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')
);
11 users liked answer #2dislike answer #211
Tharaka Wijebandara profile pic
Tharaka Wijebandara

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 !

2 users liked answer #3dislike answer #32
Mustapha GHLISSI profile pic
Mustapha GHLISSI

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))
 );
2 users liked answer #4dislike answer #42
Harvester Haidar profile pic
Harvester Haidar

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.

1 users liked answer #5dislike answer #51
Mohd Nowman profile pic
Mohd Nowman

Copyright © 2022 QueryThreads

All content on Query Threads is licensed under the Creative Commons Attribution-ShareAlike 3.0 license (CC BY-SA 3.0).