React traps · Chapter 11 of 16

An object in effect dependencies

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

On its own that is waste. It becomes a loop when the effect itself causes a render: it asks the server, the answer goes into state, state re-renders, the render makes a new object, the effect runs, and it asks the server again. Nothing errors. Each request is fine; there are just endlessly many, one per round trip.

The fix is to depend on the plain values, the show's id and the day, and make the object inside the effect, where it is new only when the effect runs. The same price tag with [rupees]:

effect ran for 180

useMemo can also keep one object across renders until its values change; plain values are simpler.

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: start the request loop on purpose

    Write reproduce() in repro.jsx. Build a fake getShowtimes that counts its calls and does not answer straight away: each call returns a promise and keeps its resolve in an array. Render ShowtimeList with any showId and day and your fake. Then, three times, inside an awaited act, answer every call that is waiting (with a list of times, like [{ id: 'T0023', label: '9:30 pm', venue: 'Kala Talkies' }]). Resolve to how many times getShowtimes was called.

    The check runs your reproduce() twice: beside the original ShowtimeList.jsx it must resolve to 4 (every answer asked again), and beside a fixed ShowtimeList.jsx to 1. So answer exactly three rounds, and only the calls that are waiting. Never a fake that answers at once: the loop then runs thousands of times. No real waiting: reproduce() must finish within 5 seconds.

  2. Task 2

    ShowtimeList.jsx: one request per show and day

    Fix ShowtimeList({ showId, day, getShowtimes }) in ShowtimeList.jsx. It asks getShowtimes({ showId, day }) on its first render, and once more whenever the show or the day changes, with the new values. An answer arriving asks nothing new, and neither does a re-render with the same show and day. It shows an h3 Showtimes on and the day, and one li for each time of the latest answer, like 9:30 pm · Kala Talkies.

    Keep the heading, the list and the props as they are. The check holds the server's answers back and lets them through a round at a time, and counts the requests; a fix that asks only once, ever, so a new day keeps the old times, is as wrong as the bug.

  3. Task 3

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

    Write a vitest test file, ShowtimeList.test.jsx, that would have caught this bug before the release. The check runs your file three times: beside the original ShowtimeList.jsx, where at least one test must fail; beside the fixed ShowtimeList.jsx, where every test must pass; and beside a wrong fix with an empty dependency list, which asks only once, ever, where at least one test must fail.

    So test both sides with a fake whose answers you hold back and give in rounds: after a round or two of answers there is still one request; and after a rerender with another day there is a request for the new day. Never answer at once: beside the original code that loops thousands of times and can stall the run. No real waiting: each test must finish within 5 seconds.

What you will learn

  • repro.jsx: start the request loop on purpose
  • ShowtimeList.jsx: one request per show and day
  • ShowtimeList.test.jsx: a test that would have caught it

More in Effects

Build "An object in effect dependencies" 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.