React traps · Chapter 16 of 16
A context value made on every render
Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.
- Part: Effects
- 3 tasks
- React
- In the all labs plan
The first 2 chapters of React traps are free. This one comes with the all labs plan.
Why you need this
memo skips a render when the parent renders with equal props. A context change does not come through the parent: React goes straight to every component that reads that context. So the seat map's memo holds back nothing here, and nothing errors. It is just slow, once a second, on the biggest component on the page.
The value has to stay the same object until its contents change. useMemo keeps the object until something in its list changes, and useCallback keeps a function the same way (the case's addSeat is a function made on every render, too):
with useMemo: CityName rendered 1 times for 3 banners
A value memoised with an empty list stops the renders and the updates with them: a reader would never see a new seat. Moving the clock into its own small component is another good fix.
What you build: 3 tasks
Each task has tests. You write the code in the lab's editor and press Check.
- Task 1
repro.jsx: render the seat map every second on purpose
Write reproduce() in repro.jsx. Turn on fake timers with vi.useFakeTimers(). Write a small component that reads the basket with useBasket() and adds 1 to a counter every time it renders. Render it inside BasketProvider, then move the clock on one second at a time, five times, each second in its own act (act(() => vi.advanceTimersByTime(1000))). Resolve to how many times your component rendered.
The check runs your reproduce() twice: beside the original BasketContext.jsx it must resolve to 6 (one first render, then one for each tick), and beside a fixed BasketContext.jsx to 1. So the five seconds must pass in five separate acts: React handles all the updates of one act in one render. No real waiting: reproduce() must finish within 5 seconds.
- Task 2
BasketContext.jsx: the clock ticks without rendering the seat map
Fix BasketProvider in BasketContext.jsx. When the hold clock ticks, the line Hold ends in m:ss changes (2:05, 2:04, and so on, from holdSeconds), and no component that reads the basket with useBasket() renders again. When a seat is added with addSeat, every component that reads the basket renders once more and sees the new list of seats; later ticks again render none of them; a second seat added with the same addSeat shows too.
Keep the exports BasketProvider({ holdSeconds, children }) and useBasket(), the clock line, and what useBasket() gives: { seats, addSeat }. The check uses fake timers and its own components (one of them memo), counts their renders, and then adds seats. A value that is made once and never again stops the extra renders and the new seats too: as wrong as the bug.
- Task 3
BasketContext.test.jsx: a test that would have caught it
Write a vitest test file, BasketContext.test.jsx, that would have caught this bug. The check runs your file three times: beside the original BasketContext.jsx, where at least one test must fail; beside the fixed BasketContext.jsx, where every test must pass; and beside a wrong fix whose value is made once and never again, where at least one test must fail.
So test both sides with a component that reads the basket and counts its renders: with fake timers, ticks of the clock (one second an act) do not render it again; and adding a seat (a button in your component that calls addSeat) shows the seat. No real waiting: each test must finish within 5 seconds.
What you will learn
- repro.jsx: render the seat map every second on purpose
- BasketContext.jsx: the clock ticks without rendering the seat map
- BasketContext.test.jsx: a test that would have caught it
More in Effects
Build "A context value made on every render" yourself
Parda's web app runs on React, and Rukhsana, its senior frontend engineer, keeps assigning you tickets. Each is a real trap interviews love to ask about. A real editor and terminal in your browser, AI that explains, and a Check that says when it works.
The first 2 chapters of React traps are free. This one comes with the all labs plan.