04 / 10

What is a reducer in Redux?

A reducer is a pure function responsible for handling actions and returning a new state based on the previous state and the action that was dispatched.

  1. 1

    A reducer is a function that receives the current state and an action object, decides how to update the state if necessary, and returns the new state: (state, action) => newState.

  2. 2

    You can think of a reducer as an event listener that handles events based on the received action (event) type.

Here is an example of reducer function:
Reducers must always follow some specific rules:
  1. 1

    They should only calculate the new state value based on the current state and action arguments.

  2. 2

    They are not allowed to modify the existing state. Instead, they must make immutable updates, by copying the existing state and making changes to the copied values.

  3. 3

    They must not do any asynchronous logic, calculate random values, or cause other "side effects" that is they must be pure functions

javascript
Difficulty: 5/10
Topics: state updates, pure functions, action handling

Scenario Questions

0-2 years experience
  1. 1

    You need to add a new piece of UI that toggles a boolean flag in the Redux store. How would you write the reducer for that flag?

  2. 2

    If an action with type 'INCREMENT' is dispatched but your reducer forgets to handle it, what will the state look like afterwards?

2-5 years experience
  1. 1

    While implementing a shopping cart feature, the cart total sometimes becomes stale after removing an item. Walk me through how you would debug the reducer responsible for the cart state.

  2. 2

    We want to split a large reducer into smaller ones for maintainability. What trade‑offs do you consider when using combineReducers versus a hand‑rolled switch statement?

5-8 years experience
  1. 1

    Our application now stores a deeply nested user profile object. Updating a nested field caused performance regressions. How would you refactor the reducer to minimize unnecessary re‑renders?

  2. 2

    Explain how you would design a reducer strategy to support optimistic UI updates and rollback on server errors.

8+ years experience
  1. 1

    We are migrating several legacy modules from Redux to a new state‑management library. What approach would you take to gradually replace reducers while keeping the app stable?

  2. 2

    At scale, many teams own separate slices of the store. How would you enforce consistent reducer patterns and prevent accidental state shape changes across teams?

Follow-up Questions

  • Why is immutability important in a reducer?
  • How do you test a reducer in isolation?
  • What happens if a reducer returns undefined?