React · Chapter 17 of 26

Races and cleanup

By the end of this chapter you can explain why an effect that fetches can show an answer nobody asked for, fix it with a flag in the cleanup or with an AbortController, spot the stale value an interval keeps, and poll Parda's server on a timer that stops when it should. Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.

  • Part: Effects and data
  • 3 tasks
  • React
  • In the all labs plan

The first 2 chapters of React are free. This one comes with the all labs plan.

Why you need this

Chapter 16 loaded data in an effect: a request, a loading line, the answer or an error. It works when one request is in flight. On Parda's show page there are often several.

Ira was testing the show page on Saturday. She clicked Pune 52, changed her mind and clicked The Tiffin Thief a moment later. The picker showed The Tiffin Thief as picked; the card under it showed Pune 52. Nothing threw and the console was empty. Datta knows how that ends at the box office: someone books the show they did not pick.

The search box has the same problem from the other side. Typing pune asks the server four times, for p, pu, pun and pune, and only the last answer matters; the other three still run and still arrive. And tonight's card says 27 seats left for Ghar Wapsi Express: it said that when the page opened, and people have been booking since.

Each effect does exactly what it was told. What chapter 16's effects never said is what should happen to a request when its effect is over, and that is what the cleanup is for.

What you build: 3 tasks

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

  1. Task 1 · interview question

    Interview: ShowDetails.jsx, the show that came back too late

    ShowDetails loads the show whose id is in its showId prop with getShow(showId) from api.js, as chapter 16 taught: while it waits it shows a paragraph Loading show...; then an h2 with the show's title and its tagline in a paragraph; if it fails, the error's message and a Retry button. That part works. The bug: click Pune 52, then quickly The Tiffin Thief. Pune 52 is slow, so its answer arrives last, and the card shows Pune 52 while The Tiffin Thief is picked.

    Fix it in the effect's cleanup: when showId changes, or the card goes away, an answer for the old id must change nothing (not the show, not the error, not the loading line). A flag that the cleanup sets, or an AbortController whose signal goes to getShow(showId, { signal }) and that the cleanup aborts, both work; an aborted request rejects with an AbortError, which is not an error to show.

    The Check renders ShowDetails in StrictMode with showId S02, changes it to S14, answers S14 (The Tiffin Thief) first and S02 (Pune 52) last: the h2 must still say The Tiffin Thief. Then it changes S13 to S16, answers S16 (Hamlet in Hinglish), and makes the old request for S13 fail: the card must still show Hamlet in Hinglish, with no error.

    Rukhsana will ask: what is a race condition in an effect, and what are two ways to fix it?

  2. Task 2

    SearchResults.jsx: cancel the search nobody needs

    SearchResults gets the text typed in the search box in its query prop. Each time query changes, make a new AbortController and call searchShows(query, { signal: controller.signal }) from api.js. While it waits, show a paragraph Searching... (className="loading"). When it answers with shows, show a list, a ul (className="results"), with one li for each show, in the order given, each with the show's title in it. When it answers with no shows, show a paragraph No shows match "zz". with the query between the double quotes and a full stop after them. When it fails, show the error's message in a paragraph (className="error-text").

    When query changes, or SearchResults goes away, abort the request that is still running, in the effect's cleanup. An aborted request rejects with an error whose name is "AbortError": show nothing for it, no error. The Check renders SearchResults in StrictMode with query pu, then pune: the request for pu must be aborted, and the page must still say Searching... until pune answers (Pune 52 and Puneri Patya). It also tries zz (no shows), lal (the server fails with The server is busy. Try again.) and raag, and then removes SearchResults: the search for raag must be aborted.

  3. Task 3

    LiveSeats.jsx: seats left, checked every 5 seconds

    LiveSeats gets a showtime's id in its showtimeId prop. Call getSeatsLeft(showtimeId) from api.js at once, then again every 5 seconds (an interval of 5000 ms), and show the latest answer: before the first answer, a paragraph Checking seats...; then a paragraph with the number and seats left, like 27 seats left; when the answer is 0, a paragraph Sold out. Give the paragraph className="live" for app.css (className="live sold-out" when sold out).

    When showtimeId changes, ask for the new showtime at once and from then on only for it. When LiveSeats goes away, stop asking. The Check uses fake timers and renders LiveSeats in StrictMode, which runs the effect, its cleanup and the effect again: in every 5 seconds exactly one request may be made, so only one interval may be running. It shows T0023 (27, then 26, then 25 seats left), changes showtimeId to T0038 (98, then Sold out) and checks that no request for T0023 is made after that, then removes LiveSeats and checks that no request is made in the next 20 seconds.

What you will learn

  • A race in an effect: two requests for one component, and the last answer wins, even when it is the old one.
  • The cleanup as the fix: React runs the old effect's cleanup before the next effect, so it can tell the old request it no longer matters.
  • An ignore flag: the old answer still arrives, but changes nothing.
  • AbortController: a signal for the request and abort() in the cleanup, so the old request stops, with an AbortError.
  • A stale closure in an interval: a timer that keeps the values of the render that made it (stuck at 1), and the updater that fixes it.
  • Polling: asking the server again every few seconds, with one interval, cleared when the effect is over.

More in Effects and data

Build "Races and cleanup" yourself

Parda sells tickets for Pune's own cinemas and stages, and its booking page broke on opening night. You rebuild it in React. 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 are free. This one comes with the all labs plan.