Course map
Stage 1: Foundation
Stage 2: Intermediate
Stage 3: Advanced
Stage 4: Routing and Application Architecture
Stage 5: Specialization (pick ONE)

Part 10 / Topic 3 of 8

Immutable updates inside a reducer

Every branch above returns a brand-new object - { items: [...] } - built with spread, map, or filter; none of them writes state.items.push(...) or mutates an item in place. This matters for a reason beyond style: React decides whether to re-render by comparing the previous state object to the new one, and a function that mutates the old object in place and returns that same reference would make React think nothing changed at all. examples/02_cart_reducer_pure.test.jsx checks this directly - calling cartReducer(initialCart, { type: "add", dish }) and then asserting initialCart.items is still [] proves the original state object was never touched, not just that the new one looks right.

Done here? The practice set for Part 10 — including the solutions — is on the part page.

Search the guide