Retain Redux Toolkit defaults while adding application middleware
Store setup
Redux Toolkit · Function
getDefaultMiddleware returns Redux Toolkit's standard middleware array for use in configureStore. Its options can disable or configure individual defaults, while concat and prepend support adding custom middleware.
Inside configureStore's middleware callback, invoke the supplied function to obtain Redux Toolkit's default middleware array. Pass an options object to disable a default or customize its behavior, then use array helpers such as concat or prepend to add application middleware while preserving the defaults.
function getDefaultMiddleware<S = any>(options: GetDefaultMiddlewareOptions = {}): Middleware<{}, S>[]| Name | Type | Description |
|---|---|---|
options | GetDefaultMiddlewareOptions | Optional settings for including, excluding, or configuring the default middleware; defaults to an empty object. |
An array of Redux middleware: Middleware<{}, S>[]
import { configureStore } from '@reduxjs/toolkit'
import logger from 'redux-logger'
import rootReducer from './reducer'
export const store = configureStore({
reducer: rootReducer,
middleware: (getDefaultMiddleware) =>
getDefaultMiddleware().concat(logger),
})Creates a store with Redux Toolkit's defaults and appends a logger middleware.
Store setup
Development configuration
Middleware ordering
The callback receives getDefaultMiddleware, and calling it returns Redux Toolkit's default middleware array. The callback should return the middleware array that configureStore installs.
Follow-up: How would you add logger middleware without losing thunk and development checks? Return getDefaultMiddleware().concat(logger), placing logger after the defaults.
Pass options such as immutableCheck, serializableCheck, or thunk to configure individual defaults. A corresponding false value excludes that middleware, while an options object customizes it.
Follow-up: How can you customize immutability checking while retaining every other default? Call getDefaultMiddleware({ immutableCheck: { ignoredPaths: ['somePath'] } }).
Use concat to append middleware after the defaults or prepend to place middleware before them. Ordering matters because middleware run in array order when an action is dispatched.
Follow-up: Why might prepend be preferable for a listener middleware? It lets that middleware observe the dispatch before the default chain processes it.
configureStoreTuplecreateImmutableStateInvariantMiddlewarecreateSerializableStateInvariantMiddlewareredux-thunk