React traps · Chapter 10 of 16

A missing effect dependency

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

The effect does run, sometimes: whenever a value that is in the list changes, it reads every value fresh, so the page heals itself. That is why typing a search fixed the list. Nothing errors and nothing warns at run time. The react-hooks/exhaustive-deps lint rule reads the effect and says which values are missing, but the lab has no lint, and plenty of teams switch the rule off.

The fix is to list every value from the component that the effect reads: props, state, and anything worked out from them. With [seats, showTitle]:

page: 2 seats for Raag Diwali | tab: 2 seats · Raag Diwali

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: keep Pune's shows in Nashik on purpose

    Write reproduce() in repro.jsx. Build a fake searchShows({ city, query }) that pushes the city of every request into an array and answers at once with an empty list. Render ShowList with city Pune, query '' and your fake, then rerender it with city Nashik and the same query, let the answers in with an awaited act, and resolve to the array of cities.

    The check runs your reproduce() twice: beside the original ShowList.jsx it must resolve to ['Pune'] (Nashik was never asked for), and beside a fixed ShowList.jsx to ['Pune', 'Nashik']. So change only the city. No real waiting: reproduce() must finish within 5 seconds.

  2. Task 2

    ShowList.jsx: the list follows the city and the search

    Fix ShowList({ city, query, searchShows }) in ShowList.jsx. It asks searchShows({ city, query }) on its first render, and again whenever the city changes, the search changes, or both (one request when both change in the same render). When it re-renders with the same city and search, it asks nothing new. It shows an h3 Shows in and the city, and a list with one li for each title of the latest answer.

    Keep the heading, the list and the props as they are. The check uses a server that answers at once with its own shows, changes the city, the search and both, and re-renders with the same values; a fix that follows only the city is as wrong as the bug.

  3. Task 3

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

    Write a vitest test file, ShowList.test.jsx, that would have caught this bug before the release. The check runs your file three times: beside the original ShowList.jsx, where at least one test must fail; beside the fixed ShowList.jsx, where every test must pass; and beside a wrong fix whose dependency list has only the city, so typing a search asks nothing, where at least one test must fail.

    So test both values the effect reads: rerender with another city and expect a request for it, and rerender with another search and expect a request for it. Use a fake searchShows made with vi.fn that answers at once. No real waiting: each test must finish within 5 seconds.

What you will learn

  • repro.jsx: keep Pune's shows in Nashik on purpose
  • ShowList.jsx: the list follows the city and the search
  • ShowList.test.jsx: a test that would have caught it

More in Effects

Build "A missing effect dependency" 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.