React traps · Chapter 4 of 16

&& that renders 0

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.

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: put a stray 0 on a card on purpose

    Write reproduce() in repro.jsx. Render ShowCard with a show that is sold out and has no offers: title Pune 52, venue Kala Talkies, time 9:30 pm (any venue and time will do), seatsLeft 0 and offers an empty array. Resolve to the text of the card's h2.

    The check runs your reproduce() twice: beside the original ShowCard.jsx it must resolve to 'Pune 520' (a stray 0 after the title), and beside a fixed ShowCard.jsx it must resolve to 'Pune 52'. So the title must be Pune 52 and the offers empty. reproduce() must finish within 5 seconds.

  2. Task 2

    ShowCard.jsx: no stray 0 on any card

    Fix ShowCard({ show }) in ShowCard.jsx so the card shows exactly what it should, and nothing else. The h2 holds the title, then a badge when there are offers: 1 offer for one, 2 offers, 3 offers and so on for more, and no badge at all for none. The seats line says Sold out when seatsLeft is 0, 1 seat left for 1, and 27 seats left for 27. No 0 may appear anywhere in the card.

    Keep the h2 (the title and the badge inside it), the venue line (Kala Talkies · 9:30 pm) and the words as they are. The check uses its own shows, with 0 to 3 offers and 0 to 140 seats left, and compares the card's whole text; a fix that turns the offers array itself into true or false shows a badge 0 offers for a show with none.

  3. Task 3

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

    Write a vitest test file, ShowCard.test.jsx, that would have caught this bug before it shipped. The check runs your file three times: beside the original ShowCard.jsx, where at least one test must fail; beside the fixed ShowCard.jsx, where every test must pass; and beside a wrong fix that turns the offers array into true or false, where at least one test must fail.

    So test both sides: a show with no offers has an h2 whose whole text is the title alone, and a show with offers has its badge (like 2 offers). No real waiting: each test must finish within 5 seconds.

What you will learn

  • repro.jsx: put a stray 0 on a card on purpose
  • ShowCard.jsx: no stray 0 on any card
  • ShowCard.test.jsx: a test that would have caught it

More in State and rendering

Build "&& that renders 0" 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.