React traps · Chapter 7 of 16

A hook after an early return

Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.

  • Part: State and rendering
  • 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

When the data is already there on the first render, the early return never runs, so every render calls both hooks and React is happy. Only a slow load gives a render without data and then one with it. So the bug hides on a fast machine and in any test that renders the page once.

One more surprise, measured: if the loading render returns before every hook, there is no error at all, because React treats the first render with hooks as the start. It is the hook before the return plus the one after it that breaks.

The fix is the rule of hooks: call every hook on every render, at the top, before any return, never inside an if, a loop or a callback. Values that need the data are worked out after the hooks:

first render: Loading reviews...
after rerender: 2 reviews, newest first

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: blank the show page on purpose

    Write reproduce() in repro.jsx. Render ShowPage with show null, as a slow phone sees it first, then render it again with a show, using the rerender function that render gave back. The show is an object with an id, a title and classes (each seat class and its price in rupees, like { Front: 120, Middle: 180, Balcony: 240 }). If the second render throws, resolve to the error's message; if it does not, resolve to the text of the h2.

    The check runs your reproduce() twice: beside the original ShowPage.jsx it must resolve to 'Rendered more hooks than during the previous render.', and beside a fixed ShowPage.jsx it must resolve to the show's title. So it must load the show after a render with null: a show from the very first render never fails. No real waiting: reproduce() must finish within 5 seconds.

  2. Task 2

    ShowPage.jsx: the show page survives a slow load

    Fix ShowPage({ show }) in ShowPage.jsx. While show is null it shows a paragraph Loading show.... Once a show arrives, after a loading render or not, it shows the title in an h2, a select labelled Seat class that starts at the show's cheapest class, a paragraph like 2 seats · ₹240 (the seats times the class's price) and an Add a seat button. Picking another class and adding seats still updates the line.

    It must not throw when the show arrives after a loading render, nor when show goes back to null, and React must not warn about the order of hooks. A fresh ShowPage that loads another show starts its select at that show's cheapest class. Keep the texts, the label and the props as they are. The check uses its own shows, and a fix that drops the loading line or starts the select empty is as wrong as the bug.

  3. Task 3

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

    Write a vitest test file, ShowPage.test.jsx, that would have caught this bug before the release. The check runs your file three times: beside the original ShowPage.jsx, where at least one test must fail; beside the fixed ShowPage.jsx, where every test must pass; and beside a wrong fix with no early return at all, which never shows Loading show..., where at least one test must fail.

    So test the slow load from both ends: while show is null the page says Loading show..., and after a rerender with a show its title is the h2. No real waiting: each test must finish within 5 seconds.

What you will learn

  • repro.jsx: blank the show page on purpose
  • ShowPage.jsx: the show page survives a slow load
  • ShowPage.test.jsx: a test that would have caught it

More in State and rendering

Build "A hook after an early return" 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.