Command Palette

Search for a command to run...

Hectal
Back to the lesson: Topic 3.3 — Keys: How React Tells Items Apart
React · Example 3 of 4

The missing-key warning

The warning names ProductList, the component whose render created the array. Add key={p.id} to <ProductRow> and it disappears.

A key is a label you give each item in a list so React can match it with the same item on the next render; keys must be unique among siblings and stable, which is why they come from your data's ids and not from the index or Math.random().

Change the code and press Run (Ctrl+Enter). Predict what will happen first, then break it on purpose and read what changes. Your edits are saved and match the lesson page.

Ready for more? This phase has 5 hands-on exercises with automatic checks.

Practice questions

Write the code in the editor, run it, then open the model answer to compare.

01

Fix this list so it has no warning and no state bug: todos.map((t, i) => <TodoRow key={i} todo={t} />), where each todo is { id, text }.

02

Write a NewTodo handler that adds { text } typed by the user to a todos state array with a proper key, and the matching map.

03

Render a table body from rows = [{ id, name, score }] where each item becomes two <tr> elements: one with the name and one with the score. Key it correctly.

Explain it without notes

01

What is a key in React, and why does React need one for list items?

02

What are the rules for a good key? Where should keys come from?

03

Why is using the array index as a key a problem? When is it acceptable?

04

What goes wrong with key={Math.random()}?

05

How do you key an item that renders several sibling elements without a wrapper?

The missing-key warning

Preview

Press Run to see the page.

Console

Nothing logged yet.

Expected console output

Each child in a list should have a unique "key" prop.

Check the render method of `ProductList`. See https://react.dev/link/warning-keys for more information.