Course contentsShow
← React
Lesson 761 of 2,39620. useReducerFree lesson

Default Case: Returning Current State

Always include a default case that returns the current state unchanged for unknown actions.

Default Case: Returning Current State

You'll learn why every reducer needs a safety net for unexpected actions.

Why the Default Case Matters

When you write a reducer with a switch statement, you're handling specific action types you know about. But what happens when an action you don't recognize gets dispatched? Without a default case, your reducer might return undefined, breaking your entire component.

The solution: always include a default case that returns the current state unchanged.

The Pattern

function reducer(state, action) {
  switch (action.type) {
    case 'INCREMENT':
      return { count: state.count + 1 };
    case 'DECREMENT':
      return { count: state.count - 1 };
    default:
      return state; // Safety net!
  }
}

That default case is your safety net. If someone dispatches { type: 'UNKNOWN' } or makes a typo, your reducer simply returns the existing state—no crashes, no undefined values.

Think of It Like a Receptionist

Imagine a receptionist at a company. They know how to handle specific requests: "Schedule a meeting," "Cancel an appointment," "Transfer a call." But if someone asks for something bizarre—"Paint the ceiling purple"—the receptionist doesn't shut down. They simply maintain the status quo: "I can't help with that, but everything stays as it is."

That's exactly what default: return state does.

When It Activates

The default case runs when:

  • Action type has a typo
  • You dispatch an action before adding its handler
  • Third-party code dispatches unexpected actions
  • During development and debugging

Without it, these scenarios would cause bugs that are hard to track down.

Key Takeaway: Always include default: return state in your reducer's switch statement—it's your safety mechanism ensuring unknown actions don't break your state.