How to fix Reducer returned undefined after action, check your action type in React
This error occurs when a Redux reducer returns undefined instead of a valid state. It's a common mistake in Redux applications that can be fixed by ensuring reducers always return a defined state, even for unrecognized actions.
Quick fix
Fixes Reducer returned undefined after action, check your action type
function myReducer(state = initialState, action) {
switch (action.type) {
case 'SOME_ACTION':
return { ...state, ...action.payload };
default:
return state; // Always include this default case
}
}- Check for missing default case in reducer
- Verify all action cases return a state
- Provide initial state with default parameter
- Test with combineReducers
function myReducer(state = initialState, action) {switch (action.type) {case 'SOME_ACTION':return { ...state, ...action.payload };default:return state; // Always include this default case}}Reducer returned undefined after action, check your action type
What this error means
The "Reducer returned undefined" error happens when a Redux reducer function does not return a proper state value for a dispatched action. Redux requires that reducers always return a defined state object. This error typically indicates that the reducer is missing a default case in a switch statement or has an early return statement that doesn't provide a state value.
Typical symptoms
- Redux throws an error in the console: "Reducer returned undefined when handling action"
- Application state becomes undefined, causing components to break
- UI fails to update after dispatching certain actions
- Error occurs when using combineReducers() with improperly written slice reducers
Common causes
- Missing default case in a reducer switch statement
- Forgetting to return state in one of the action cases
- Using early return statements without providing a state value
- Reducer function not providing initial state when state is undefined
- combineReducers() detecting that one of the slice reducers returned undefined
How to fix "Reducer returned undefined after action, check your action type"
Ensure every reducer has a default case that returns the current state:
function myReducer(state = initialState, action) {
switch (action.type) {
case 'SOME_ACTION':
return { ...state, ...action.payload };
default:
return state; // Always include this default case
}
}Check each case in your switch statement to ensure it returns a proper state value:
// Incorrect - missing return
case 'UPDATE_DATA':
state.data = action.data; // This doesn't return!
break;
// Correct - returns new state
case 'UPDATE_DATA':
return { ...state, data: action.data };Use ES6 default parameters to ensure the reducer always has an initial state:
// Good - provides default initial state
function reducer(state = { count: 0 }, action) {
// reducer logic
}
// Also acceptable - explicit check
function reducer(state, action) {
if (state === undefined) {
return { count: 0 };
}
// rest of reducer
}If using combineReducers, ensure all slice reducers follow the rules:
import { combineReducers } from 'redux';
const rootReducer = combineReducers({
todos: todosReducer, // Must never return undefined
visibility: visibilityFilterReducer // Must never return undefined
});
// Each reducer must handle undefined state by returning initial state
function todosReducer(state = [], action) {
// ...
}Install Redux DevTools extension and inspect the action that causes the error:
1. Open browser DevTools
2. Go to Redux tab
3. Check which action triggered the error
4. Examine the reducer handling that action
This helps identify exactly which reducer is returning undefined.
Advanced notes
Redux's combineReducers utility performs a development check that throws this error when any slice reducer returns undefined. This is a safety feature to catch common mistakes early. In production builds, this check may be omitted for performance, but the underlying issue could still cause bugs. Always ensure reducers are pure functions that never return undefined, mutate state, or have side effects.
Frequently asked questions
- What does "Reducer returned undefined after action, check your action type" mean?
- The "Reducer returned undefined" error happens when a Redux reducer function does not return a proper state value for a dispatched action. Redux requires that reducers always return a defined state object. This error typically indicates that the reducer is missing a default case in a switch statement or has an early return statement that doesn't provide a state value.
- How do I fix "Reducer returned undefined after action, check your action type"?
- This error occurs when a Redux reducer returns undefined instead of a valid state. It's a common mistake in Redux applications that can be fixed by ensuring reducers always return a defined state, even for unrecognized actions.
- What causes "Reducer returned undefined after action, check your action type"?
- Missing default case in a reducer switch statement Forgetting to return state in one of the action cases Using early return statements without providing a state value
- Is the "Reducer returned undefined after action, check your action type" error serious?
- It is a medium-severity error โ more of a blocker than a risk. The steps above resolve it.