React traps · Chapter 3 of 16

key={index} in a list

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

An index is a perfectly valid key: unique, and the same type every time. React warns only when keys are missing:

Each child in a list should have a unique "key" prop. Check the render method of `NoKeys`. See https://react.dev/link/warning-keys for more information.

With key={index} there is no warning at all. A list of plain text also looks right, because React updates the text of every row. It shows only where a row keeps something React does not redraw, like what a person typed into an uncontrolled input, and only when a row goes from anywhere but the end.

A good key comes from the data: an id, or here the seat, unique among the rows and the same for as long as the row is on the list.

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: move the names up a seat on purpose

    Write reproduce() in repro.jsx. Render TicketNames with the seats E7, E8 and E9. Type Asha into the box labelled Name on ticket for E7, Bilal into E8's and Chirag into E9's, press the Remove E7 button, and resolve to the two rows left as an array of strings, each the seat, a colon, a space and the name in its box: like 'E8: Bilal'.

    The check runs your reproduce() twice: beside the original TicketNames.jsx it must resolve to ['E8: Asha', 'E9: Bilal'] (the names moved up a seat), and beside a fixed TicketNames.jsx it must resolve to ['E8: Bilal', 'E9: Chirag']. So it must really type, remove and read the boxes. reproduce() must finish within 5 seconds.

  2. Task 2

    TicketNames.jsx: every name stays with its seat

    Fix TicketNames({ seats }) in TicketNames.jsx. Whatever row is removed, the first, a middle one or the last, its row goes, and every other row keeps the name typed in it and keeps its own input element: React must not move one row's input under another row's label or make a new one.

    The inputs stay uncontrolled (the booking form reads them when it is sent). Keep the labels (Name on ticket for E7), the buttons (Remove E7) and the input names as they are, and React must print no warning. The check uses its own seats and names, removes the first, a middle and the last row, and compares each box with the element it was before; a key that is new on every render makes every input new, so the names vanish.

  3. Task 3

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

    Write a vitest test file, TicketNames.test.jsx, that would have caught this bug before it shipped. The check runs your file three times: beside the original TicketNames.jsx, where at least one test must fail; beside the fixed TicketNames.jsx, where every test must pass; and beside a wrong fix whose key is new on every render, where at least one test must fail.

    So type a name into every row, remove a row that is not the last, and expect each name left to be in its own seat's box: not moved, and not gone. No real waiting: each test must finish within 5 seconds.

What you will learn

  • repro.jsx: move the names up a seat on purpose
  • TicketNames.jsx: every name stays with its seat
  • TicketNames.test.jsx: a test that would have caught it

More in State and rendering

Build "key={index} in a list" 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.