Blog>
Snippets

Converting applyMiddleware to the New Middleware API

Show how to transition from the old applyMiddleware usage to the new middleware API in Redux v5.0.0.
// Old applyMiddleware usage
import { createStore, applyMiddleware } from 'redux';
import thunk from 'redux-thunk';
import rootReducer from './reducers';

const store = createStore(
  rootReducer,
  applyMiddleware(thunk)
);
This is an example of how applyMiddleware was used in a Redux configuration prior to version 5.0.0, where `thunk` middleware is applied to the Redux store.
// New Redux middleware API usage
import { configureStore } from '@reduxjs/toolkit';
import rootReducer from './reducers';

const store = configureStore({
  reducer: rootReducer,
  middleware: (getDefaultMiddleware) => getDefaultMiddleware().concat(/* your custom middleware here */)
});
This example shows the new way of applying middleware using Redux Toolkit's `configureStore` function in Redux version 5.0.0, which replaces the old `applyMiddleware` function.
import { configureStore } from '@reduxjs/toolkit';
import thunk from 'redux-thunk';
import rootReducer from './reducers';

// Using Redux Toolkit's configureStore to add thunk middleware
const store = configureStore({
  reducer: rootReducer,
  middleware: (getDefaultMiddleware) => getDefaultMiddleware().concat(thunk)
});
In this particular piece of code, we have updated the store configuration to use the new middleware API with `configureStore` from Redux Toolkit, and we're adding the `thunk` middleware to the default middleware provided by `configureStore`.