React traps · Chapter 9 of 16

A stale closure in setInterval

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

Nothing is wrong as far as JavaScript knows: the interval runs, the state is set, React renders. The page shows one second gone and then nothing changes, so it looks like a slow timer. The lint rule for effect dependencies would warn about seconds, but nothing in the browser does.

Two fixes work. The updater, setSeconds((s) => s + 1), asks React for the current value, so one interval for life is fine:

after 3 s: On this page for 3 s

Or put the value in the dependencies, so the effect clears the old interval and starts a new one after every change. Any check made inside the callback (has the time run out?) has the same problem, so look at every value the callback reads, not only the one it sets.

What you build: 3 tasks

Each task has tests. You write the code in the lab's editor and press Check.

  1. Task 1

    repro.jsx: freeze the hold timer on purpose

    Write reproduce() in repro.jsx. Turn on vitest's fake timers, render HoldTimer with seconds 600 and an onExpire that does nothing, move the fake clock on 5 seconds inside act, and resolve to the text of the timer's paragraph.

    The check runs your reproduce() twice: beside the original HoldTimer.jsx it must resolve to 'Seats held for 9:59' (stuck), and beside a fixed HoldTimer.jsx it must resolve to 'Seats held for 9:55'. So 5 seconds must pass on the fake clock after render. No real waiting: the fake clock moves only when you say, and reproduce() must finish within 5 seconds.

  2. Task 2

    HoldTimer.jsx: the hold timer counts down to the end

    Fix HoldTimer({ seconds, onExpire }) in HoldTimer.jsx. It shows a paragraph Seats held for and the time left as minutes and two-digit seconds (1:35, 0:09), and the time goes down by one every second. When it reaches zero it shows Hold expired instead and calls onExpire() once, and never again however long the page stays open.

    When HoldTimer goes away, no timer may be left running, and in StrictMode it must still count one second per second. Keep the texts and the props as they are. The check uses fake timers and its own seconds, moves the clock one second at a time, and runs it to zero: a fix that counts down but never calls onExpire is as wrong as the bug.

  3. Task 3

    HoldTimer.test.jsx: a test that would have caught it

    Write a vitest test file, HoldTimer.test.jsx, that would have caught this bug before the release. The check runs your file three times: beside the original HoldTimer.jsx, where at least one test must fail; beside the fixed HoldTimer.jsx, where every test must pass; and beside a wrong fix that counts down but whose expiry check still reads the first render's time, so it never calls onExpire, where at least one test must fail.

    So test both ends with fake timers: a few seconds in, the time shown has gone down by that many seconds; and after the time is up, Hold expired shows and onExpire was called once. No real waiting: each test must finish within 5 seconds.

What you will learn

  • repro.jsx: freeze the hold timer on purpose
  • HoldTimer.jsx: the hold timer counts down to the end
  • HoldTimer.test.jsx: a test that would have caught it

More in Effects

Build "A stale closure in setInterval" 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.