What is a reducer — and why the screen didn't update even though the data changed

You tap "+" in the cart. The console says the count went up to three. The screen still says two.
The AI wrote the code, and it looks fine:
function cartReducer(state, action) {
if (action.type === 'add') {
state.count = state.count + 1
return state
}
return state
}
Here's the sneaky part. The data did change. But the reducer returned the exact same object it received. React compares the old and new state by reference, sees "same object" — and decides there's nothing to re-render. Let's unpack what a reducer is and the two rules it demands.
What a reducer is
A reducer is a function that takes the current state and one event, and returns the new state.
(state, event) → new state
That's it. No magic. Picture a cash register. It knows the receipt total. You scan an item — the total changes. You apply a discount — it changes again. The register doesn't remember how you got to that total. It just applies each event to the current result.
The word comes from the array method reduce — "fold down". It folds a whole array into one value:
const prices = [100, 250, 50]
const total = prices.reduce((sum, price) => sum + price, 0)
// 400
The function inside reduce is the reducer. sum is the state, price is the next event, 0 is where you start. It's an ordinary callback that reduce calls for every element.
Where you'll meet it
In React — useReducer. When a component has lots of related state (a cart, a five-field form, wizard steps), AI often writes one reducer instead of five useState calls:
const [state, dispatch] = useReducer(cartReducer, { count: 0, items: [] })
dispatch({ type: 'add', item: book })
dispatch means "ring up an event at the register". You don't change the state yourself. You describe what happened, and the reducer decides what the state became.
In Redux, Zustand and similar libraries — same idea, just for the whole app's state.
In data processing — good old reduce: sum things up, build an object from a list, find the max.
Rule one: return a new object
Back to the broken cart. Here's the fix:
function cartReducer(state, action) {
if (action.type === 'add') {
return { ...state, count: state.count + 1 }
}
return state
}
{ ...state } creates a new object and copies the old fields into it. Different reference — React sees the change and re-renders.
Why so strict? Comparing references is instant; walking the whole object and comparing every field is expensive. React picked the fast path. In return it asks: don't touch the old, hand back the new. We've covered separately why an object in a variable is a reference, not a copy.
Same trap with arrays. state.items.push(item) changes the old array. The right way is [...state.items, item].
There's a flip side. If an event changes nothing, return state as is. A new object for no reason means a pointless re-render.
Rule two: a reducer does nothing "on the side"
A reducer should only compute the new state. No sending requests. No writing to localStorage. No Math.random() or current time.
Why? In development mode React deliberately calls the reducer twice (that's StrictMode). If there's a server request inside, it goes out twice. If there's a random number inside, the two calls disagree, and you're chasing a ghost bug.
Requests and saving belong in event handlers and effects. A reducer is pure arithmetic.
The empty-array trap
Now about reduce. Watch what happens if you forget the starting value:
[100, 250].reduce((a, b) => a + b) // 350 — works
[].reduce((a, b) => a + b) // TypeError
We checked in Node 22: on an empty array with no second argument, reduce throws Reduce of empty array with no initial value. Everything worked while the cart had items. A new user arrives with an empty cart — the page crashes.
The fix is one argument: always pass a starting value.
items.reduce((sum, item) => sum + item.price, 0)
How to check a reducer the AI wrote
Three questions about the code — and half the bugs are found:
- Is there
state.something = ...orpush? The old state is being mutated. Ask for a rewrite with.... - Is there
fetch,localStorage,Date.now()inside? Move it out. - Does every
reducehave a second argument? If not, add one.
You can hand it straight to the AI: "Check this reducer: it must not mutate state and must not have side effects. Show the fixed version."
Reducer or useState — which to pick?
If your state is one or two independent values, useState is simpler. If there are many fields and one event changes several at once, a reducer keeps all the logic in one place — easier to read and test.
Why does the console show the new value but the screen the old one?
You changed the object in place, and React compared references and saw no difference. Return a new object with { ...state } or a new array with [...items].
Is a reducer the same thing as Redux?
No. A reducer is just a function pattern. Redux is a library built on reducers. You can use reducers every day and never install Redux.
The AI Architect 2027 course: step by step from computer basics to your own app and first customers. 543 short lessons on your phone.





