Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

Redux Toolkit · Module

builder.addDefaultCase

builder.addDefaultCase registers a fallback reducer for actions not handled by a specific case or matcher. It is used in the builder callback passed to createReducer and can be chained with other builder methods.

How it works

Call addDefaultCase while constructing a reducer with the builder callback. Its reducer runs only when the dispatched action matched neither an explicit case nor any matcher; otherwise, the existing state is preserved or another handler updates it.

Syntax

builder.addDefaultCase(reducer)

Parameters

NameTypeDescription
reducerCaseReducer<State, Action>Reducer function used as the fallback when no case or matcher reducer handles the action.

Returns

The builder object, allowing chained reducer-builder calls.

Key points

  • It provides fallback state logic when the action bypasses every registered case and matcher.
  • Use it inside the callback form of createReducer, where the builder exposes reducer-registration methods.
  • The method takes one reducer callback, which receives the state and action like other case reducers.
  • A matched case or matcher prevents the fallback from running.
  • Without a default reducer, unmatched actions leave state untouched.

Common mistakes

  • A builder supports only one default-case reducer.
  • Register all case reducers before registering the default case.
  • Register matcher reducers before the default case as well.

Example: Fallback reducer for unmatched actions

import { createReducer } from '@reduxjs/toolkit'

const reducer = createReducer({ otherActions: 0 }, (builder) => {
  builder.addCase('counter/increment', (state) => {
    state.otherActions = 0
  })
  builder.addDefaultCase((state) => {
    state.otherActions += 1
  })
})

console.log(reducer(undefined, { type: 'counter/increment' }))
console.log(reducer(undefined, { type: 'counter/unknown' }))

The first action is handled by its specific case, while the unknown action uses the fallback reducer.

When to use it

Track unhandled actions

Increment a diagnostic counter whenever an action is not recognized by a reducer.

Handle miscellaneous actions

Apply a generic fallback transformation after excluding all known action types and matchers.

Preserve state by default

Leave state unchanged for unmatched actions by omitting a default reducer entirely.

Interview questions

QWhen does builder.addDefaultCase run?

addDefaultCase registers a fallback reducer for actions that were not handled by any case reducer or matcher. It is analogous to a default branch in a switch, but is configured through the reducer builder.

Follow-up: What happens if no case, matcher, or default reducer handles an action? The reducer returns the original state unchanged.

QWhat does the callback passed to addDefaultCase receive?

Its argument is a reducer function that receives the current state and the action. It is invoked only when no explicit case reducer and no matcher reducer ran for the action.

Follow-up: Can the fallback run after a matcher? No; if a matcher reducer executes, the default reducer does not execute for that action.

QWhat builder-order rules apply to addDefaultCase?

The method can be chained with other builder methods, but the registration order is constrained. Case reducers and matchers must be added before the default case, and only one default case is allowed.

Follow-up: How would multiple fallback behaviors be represented? Combine their logic in one default reducer or use matchers for distinct conditions before it.

  • createReducer
  • builder.addCase
  • builder.addMatcher
  • extraReducers

Source: createReducer | Redux Toolkit. Checked 2026-10-15.