Part 10 / Topic 1 of 8
The problem: several useState calls that can drift
A cart needs to describe one idea - "what's in it right now" - but it is tempting to reach for one useState per field that idea has: the item list, a displayed count, a running total. Nothing forces those three calls to move together. examples/01_cart_without_reducer_bug.jsx's BuggyCartMultiState updates all three on "Add" but only two of them on "Remove last":
// examples/01_cart_without_reducer_bug.jsx
function handleAdd(dish) {
setItems((current) => [...current, dish]);
setCartTotal((current) => current + dish.price);
// BUG: itemCount is never updated here.
}
Clicking "Add Paneer Butter Masala" twice really grows the real item list to 2 and the total to ₹440 - but the displayed count field stays at 0 forever (examples/01_cart_without_reducer_bug.test.jsx). Nothing here is a syntax error; it is a one-line omission in a function that otherwise reads correctly, which is exactly why it is easy to ship and hard to spot in review. This is the felt problem the rest of this part responds to: collapse the three related pieces into one state value, updated in exactly one place.
Done here? The practice set for Part 10 — including the solutions — is on the part page.