01 / 13

What is a reducer function?

A reducer function in Redux Toolkit is a pure function that determines how the application's state changes in response to dispatched actions. It takes the current state and an action as arguments and returns a new state based on the action type and payload.

Key Characteristics of a Reducer Function
  1. 1

    Reducers are pure functions — they do not mutate the original state or cause side effects.

  2. 2

    Each reducer listens for specific action types to decide how to update the state.

  3. 3

    Reducers return the next state based solely on the current state and the dispatched action.

Example: Basic Reducer Function in Redux Toolkit

In this example, each function inside the reducers field is a reducer that modifies the state based on a specific action. Redux Toolkit uses the Immer library internally, allowing you to write reducers that 'mutate' state safely.