combineReducers behavior when state slices return undefined during restoration
0 reputation · 26 Jun 2026, 20:01 UTC
0 reputation · 26 Jun 2026, 20:01 UTC
When implementing state restoration logic in Redux, the behavior of combineReducers regarding undefined return values can impact the integrity of the recovered state tree.
In standard operation, if a reducer returns undefined for a specific key, that key may be omitted from the resulting state object. This creates a discrepancy between the expected schema of a restored backup and the actual state tree managed by the store.
Given that combineReducers expects each reducer to return a valid state slice or its defined default initial state, the uncertainty lies in how the store handles the transition when a restoration action triggers a reducer to return undefined instead of a fallback value.
combineReducers maintain the previous state slice if the reducer returns undefined during a restoration dispatch?undefined returns in Redux 4.x and 5.x?No. This is confirmed runtime behavior in Redux 4.x and 5.x: if a slice reducer returns undefined during a restoration dispatch, combineReducers throws an Error for that key. It does not retain the previous slice and does not omit the key from the next state object.
combineReducers treats undefined from a slice reducer as an invalid response to an action. The only undefined input a reducer is expected to handle is initialization. When a restored backup lacks a key, combineReducers passes undefined to that slice reducer. If the reducer forwards that undefined back, the store throws rather than falling back.
Make each slice reducer own its fallback explicitly:
function authReducer(previousState = initialState, action) {
if (action.type === 'RESTORE') {
return action.payload.auth === undefined
? previousState
: action.payload.auth;
}
return previousState;
}
If restoration lives in a root reducer or middleware, normalize the backup before dispatch so every combineReducers key resolves to a defined slice.
combineReducers({ a: (s = 0, action) => action.type === 'RESTORE' ? undefined : s }). Dispatch RESTORE and confirm the throw.action.type === 'RESTORE' ? (restored.a === undefined ? s : restored.a) : s. Dispatch again and confirm the prior slice is kept when the backup key is absent.node_modules/redux/package.json and run the probe against your installed version. If using RTK or persistence middleware, test rehydration separately.The installed package version is the one detail that would change this recommendation if you are using a wrapper or fork.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.