Sub topic 6
Rendering the list with stable keys
This chapter assumes you know JavaScript's map method and have the App component from the earlier chapters, which keeps a todos array in state. Each to-do is an object with id, title, and done.
Rendering an array with map
JSX can hold any expression, including an array of elements. Calling map on todos turns each object into an <li>:
function TodoList({ todos }) {
return (
<ul>
{todos.map((todo) => (
<li key={todo.id}>{todo.title}</li>
))}
</ul>
);
}The key prop is the only new part. React uses it but does not pass it to your component, so props.key is always undefined.
What a key does
When state changes, React renders the list again and compares the new elements with the previous ones. Keys tell it which new element corresponds to which old one. With matching keys, React keeps the existing DOM node and any component state, and only applies the differences. Without matching keys, it falls back to comparing by position.
flowchart TDWhy the index is a poor key
Using the array index looks convenient:
{todos.map((todo, index) => (
<li key={index}>{todo.title}</li>
))}It works until the list changes order or length. Suppose you delete the first item. The old second item now has index 0, so React treats it as the old first item and reuses that row's DOM node. Anything stored with the node, such as a focused input, a half-typed edit, or the state of a child component, stays at position 0 and appears to belong to the wrong to-do. In your app this shows up once you add inline editing, because the edit text would jump to a different row.
Choosing a key
| Key source | Verdict |
|---|---|
| `todo.id` created once when the item is added | Good. It is unique and never changes. |
| Array index | Only safe for static lists that never reorder or change length. |
| `Math.random()` during render | Bad. The key changes every render, so React recreates every node. |
| `todo.title` | Risky. Titles can repeat and can change when you edit. |
Generate the id when you create the item, as in crypto.randomUUID() inside addTodo, and store it with the object. Do not generate it while rendering.
Common mistakes
- Putting the key on an element inside your component instead of on the element returned by
map. The key belongs on the outermost element in the loop. - Duplicate keys among siblings. React logs a warning and may drop or duplicate items on updates.
- Wrapping each item in a Fragment without a key. Use
<Fragment key={todo.id}>, because the short<>syntax cannot take a key.
Practice
Exercise
A list uses key={index}. A user types "Call Sam" in an editable row's text box, then deletes the row above it. Why might "Call Sam" end up in the wrong row, and what change prevents it?
Show answer
With index keys, deleting the row above shifts every later item up one index. React matches by key, so it reuses the DOM node at each index, and the text box state stays at the old position while the data moves. Using a stable todo.id as the key makes React follow each item, so its text box moves or disappears with it.