React traps · Chapter 14 of 16
Strict Mode runs effects twice
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
Production runs the effect once, so the double hold never happens there. But the effect still has nothing to undo it: when the customer backs out of checkout, or picks other seats, the hold stays until the box office's ten minutes run out. StrictMode's extra run is a rehearsal of exactly those moments.
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: hold the seats twice on purpose
Write reproduce() in repro.jsx. Build a fake boxOffice: hold(showtimeId, seats) answers at once with a new hold id (H1, H2 and so on) and keeps that id in a list of active holds; release(holdId) takes it off the list. Render SeatHold for showtimeId T0023 and seats 'E7,E8' with your fake inside StrictMode, as Parda's site runs in development, let the answers arrive with an awaited act, and resolve to how many holds are active.
The check runs your reproduce() twice: beside the original SeatHold.jsx it must resolve to 2 (the same seats held twice), and beside a fixed SeatHold.jsx to 1. So it must render inside StrictMode. No real waiting: your fake answers at once, and reproduce() must finish within 5 seconds.
- Task 2
SeatHold.jsx: one hold, released when it is no longer needed
Fix SeatHold({ showtimeId, seats, boxOffice }) in SeatHold.jsx so exactly the holds still needed stay active. While SeatHold is on the page, also in StrictMode, one hold is active, for its seats. When the seats or the showtime change, the old hold is released with boxOffice.release(holdId) and a new one is made. When SeatHold goes away, its hold is released, even a hold whose answer arrives after it has gone (release it as soon as it arrives).
Keep the lines as they are: Holding your seats... until the hold for the current seats answers, then Seats C3,C4 held for 10 minutes. The check renders in StrictMode, holds back the box office's answers and gives them one by one, changes the seats, and removes SeatHold before and after the answers. A fix that skips StrictMode's second run with a ref leaves one hold, but never releases it: as wrong as the bug.
- Task 3
SeatHold.test.jsx: a test that would have caught it
Write a vitest test file, SeatHold.test.jsx, that would have caught this bug. The check runs your file three times: beside the original SeatHold.jsx, where at least one test must fail; beside the fixed SeatHold.jsx, where every test must pass; and beside a wrong fix that skips StrictMode's second run with a ref, where at least one test must fail.
So test both sides with a fake boxOffice that counts active holds: rendered inside StrictMode, after the answers, exactly one hold is active; and after unmount, no hold is active. No real waiting: each test must finish within 5 seconds.
What you will learn
- repro.jsx: hold the seats twice on purpose
- SeatHold.jsx: one hold, released when it is no longer needed
- SeatHold.test.jsx: a test that would have caught it
More in Effects
Build "Strict Mode runs effects twice" 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.