06 / 12

How can you handle multiple actions in one reducer?

In Redux Toolkit, a single reducer can respond to multiple action types using the builder.addMatcher() or by chaining multiple .addCase() calls within extraReducers. This allows you to handle related actions with shared logic efficiently.

Ways to Handle Multiple Actions in One Reducer
  1. 1

    Use multiple .addCase() calls to handle specific actions separately but with similar logic.

  2. 2

    Use .addMatcher() to handle multiple actions that match a condition, like all actions ending with /pending.

  3. 3

    Group related actions to reduce code duplication and improve maintainability.

Example 1: Handling Multiple Actions Using addCase()
Example 2: Handling Multiple Actions Using addMatcher()