React traps · Chapter 13 of 16

An effect with no cleanup

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

A listener for an old venue is still a working function, and calling setText on a component that is still on the page is a fine update. Even after the component has gone, React no longer warns: measured, a setState after unmount prints nothing. The cost shows only as wrong numbers now and then, and a list of listeners that grows all day.

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: let the old showtime in on purpose

    Write reproduce() in repro.jsx. Build a fake feed: an object whose subscribe(showtimeId, listener) keeps the listener with its showtime and returns a function that removes it, whose unsubscribeAll() removes every listener, and with a way for you to send an update, a number of seats left, to every listener of one showtime. Render LiveSeats for T0023 with your feed, then rerender it for T0038. Inside act, send T0038 the number 98, then T0023 the number 26. Resolve to the text of the seat line.

    The check runs your reproduce() twice: beside the original LiveSeats.jsx it must resolve to '26 seats left' (T0023's number under T0038), and beside a fixed LiveSeats.jsx to '98 seats left'. So the old showtime's update must come last. No real waiting: you send the updates, and reproduce() must finish within 5 seconds.

  2. Task 2

    LiveSeats.jsx: listen to one showtime, and stop when done

    Fix LiveSeats({ showtimeId, feed }) in LiveSeats.jsx so it listens to the feed for its showtime only. When showtimeId changes, it stops listening to the old showtime and listens to the new one, once. When LiveSeats goes away, it stops listening. An update for the showtime it shows sets the line (12 seats left, or Sold out at 0); an update for any other showtime changes nothing. Before the first update it shows Checking seats...

    Stop only what this LiveSeats started: other parts of the kiosk listen to the same feed, and the check keeps a listener of its own that must still hear its updates, so a cleanup that calls unsubscribeAll is as wrong as the bug. The check counts the feed's listeners after a switch, after LiveSeats goes away, and in StrictMode, which runs the effect, its cleanup and the effect again: one listener there, none after.

  3. Task 3

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

    Write a vitest test file, LiveSeats.test.jsx, that would have caught this bug. The check runs your file three times: beside the original LiveSeats.jsx, where at least one test must fail; beside the fixed LiveSeats.jsx, where every test must pass; and beside a wrong fix whose cleanup stops every listener on the feed, where at least one test must fail.

    So test both sides with a fake feed that has subscribe, unsubscribeAll and a way to send updates: after a switch from one showtime to another, an update for the old showtime changes nothing; and a listener of your own on the same feed still hears its updates after LiveSeats switched or went away. No real waiting: each test must finish within 5 seconds.

What you will learn

  • repro.jsx: let the old showtime in on purpose
  • LiveSeats.jsx: listen to one showtime, and stop when done
  • LiveSeats.test.jsx: a test that would have caught it

More in Effects

Build "An effect with no cleanup" 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.