Redux Toolkit serializableCheck configuration when migrating from legacy Redux to RTK createSlice
0 reputation · 19 May 2023, 23:58 UTC
0 reputation · 19 May 2023, 23:58 UTC
When migrating a codebase from legacy Redux to Redux Toolkit's createSlice, the default serializableCheck middleware in configureStore begins to emit warnings for action payloads that contain non‑serializable values such as Date objects, class instances, or custom metadata.
The objective is to adjust the check so that it ignores those specific payloads (or particular action types) without disabling the middleware entirely, thereby preserving its ability to catch genuine serialization problems elsewhere in the store.
serializableCheck be configured to exclude certain action types (e.g., thunk‑generated actions) while keeping the check active for all other actions?35775 reputation · 20 May 2023, 10:56 UTC
Keep the serializableCheck middleware active while silencing warnings for specific action types or payload shapes by customizing getDefaultMiddleware options in configureStore.
getDefaultMiddleware().serializableCheck options object with ignoreActions (array of strings or RegExp) and ignorePaths (array of dot‑notation state or action paths).redux‑persist.createSlice, thunk‑generated actions can be matched with patterns like /.*\/pending/, /.*\/fulfilled/, /.*\/rejected/ (or any custom prefix) and added to ignoreActions.import { configureStore, getDefaultMiddleware } from '@reduxjs/toolkit';
const ignoreActions = [
/.*\/pending/, // matches all pending thunk actions
/.*\/fulfilled/,
/.*\/rejected/,
'mySlice/specificAction' // exact action type to skip
];
const ignorePaths = [
'payload.timestamp', // skip checking a Date inside payload
'state.meta' // skip a meta slice of state
];
getDefaultMiddleware when creating the store:const store = configureStore({
reducer: {
/* your reducers */
},
middleware: (getDefault) =>
getDefault({
serializableCheck: {
ignoreActions,
ignorePaths
}
})
});
ignoreActions; a warning should still appear, confirming the middleware remains active elsewhere.redux‑persist, rehydrate the store and ensure persisted state does not contain the ignored non‑serializable values (or that you have serialized them, e.g., converting Dates to ISO strings, before dispatch).The serializableCheck middleware walks through every dispatched action and every state change, throwing a warning when it encounters non‑serializable values. By supplying ignoreActions you tell it to skip entire action types; by supplying ignorePaths you tell it to skip specific nested fields inside actions or state. This lets you keep the safety net for the rest of the application while accommodating legacy patterns that temporarily emit non‑serializable data (such as Date objects or class instances) during migration.
If you are using a persistence library, confirm whether the ignored values need to be serialized before persisting (e.g., convert Dates to ISO strings) to avoid storage issues. This detail does not change the configuration steps but affects whether the ignored data is safe to persist.
Use comments to ask for clarification. Post a solution as an answer.
35,775 reputation · 20 May 2023, 09:28 UTC
The existing answer mentions ignoreActions and ignorePaths, but RTK's serializableCheck actually exposes three separate arrays (RTK 1.9+):
"users/fetch/pending"."payload.createdAt", "meta.requestInstance") to exempt only those nested fields while still validating the rest of the action."ui.modal.instance") for reducers that intentionally store non-serializable refs.Using ignoredActionPaths instead of broad ignoredActions keeps the check active for the action's other fields, reducing the risk of missing a genuine serialization bug.
The warnAfter option (default 32 ms) controls the timeout before a warning fires. In slow CI containers, raise it (e.g., warnAfter: 128) to avoid false positives without weakening the check.
Dispatch a test action containing a Date in a path listed in ignoredActionPaths; open the console and confirm no yellow warning appears. Temporarily remove the path and verify the warning returns—this proves the exemption is scoped correctly.