# Serializability MiddlewareA custom middleware that detects if any non-serializable values have been included in state or dispatched actions, modeled after redux-immutable-state-invariant. Any detected non-serializable values will be logged to the console.
This middleware is added to the store by default by configureStore and getDefaultMiddleware .
You can customize the behavior of this middleware by passing any of the supported options as the serializableCheck value for getDefaultMiddleware.
# OptionsCopy interface SerializableStateInvariantMiddlewareOptions {
isSerializable ? : ( value : any ) => boolean
getEntries ? : ( value : any ) => [ string , any ] [ ]
ignoredActions ? : string [ ]
ignoredActionPaths ? : string [ ]
ignoredPaths ? : string [ ]
warnAfter ? : number
}
# Exports# createSerializableStateInvariantMiddlewareCreates an instance of the serializability check middleware, with the given options.
You will most likely not need to call this yourself, as getDefaultMiddleware already does so.
Example:
Copy
export default function ( state = { } , action : any ) {
return state
}
import { Iterable } from 'immutable'
import {
configureStore ,
createSerializableStateInvariantMiddleware ,
isPlain
} from '@reduxjs/toolkit'
import reducer from './reducer'
const isSerializable = ( value : any ) =>
Iterable . isIterable ( value ) || isPlain ( value )
const getEntries = ( value : any ) =>
Iterable . isIterable ( value ) ? value . entries ( ) : Object . entries ( value )
const serializableMiddleware = createSerializableStateInvariantMiddleware ( {
isSerializable ,
getEntries
} )
const store = configureStore ( {
reducer ,
middleware : [ serializableMiddleware ]
} )
# isPlainChecks whether the given value is considered a "plain value" or not.
Currently implemented as:
Copy
declare function isPlainObject ( value : unknown ) : value is object
export default isPlainObject
import isPlainObject from './isPlainObject'
export function isPlain ( val : any ) {
return (
typeof val === 'undefined' ||
val === null ||
typeof val === 'string' ||
typeof val === 'boolean' ||
typeof val === 'number' ||
Array . isArray ( val ) ||
isPlainObject ( val )
)
}
This will accept all standard JS objects, arrays, and primitives, but return false for Dates, Maps, and other similar class instances.