Rendering lists
Understand the idea
Showing one person is easy. Showing a list means repeating a piece of UI for each item — and giving React a way to tell those items apart when the list changes.
Turn an array into UI
In plain JavaScript you loop an array with for or map. In React you still use map, but each trip through the loop returns JSX instead of pushing into a new array of numbers.
In the live example, people is an array of objects. Each object has id and name. App maps that array to list item elements. The preview shows Ada and Grace as a bullet list.
Read it as: for each person, draw one list item that shows that person’s name.
Why React asks for a key
When the list can change - sort, delete, insert - React needs to know which old item matches which new item. The key prop is that identity.
Pass key on the element map returns. In the live example, each list item uses person.id as its key.
The key is not something your component usually reads as a prop. It is an instruction to React’s : “this child is the Ada row,” not “whichever child is currently in slot 0.”
Use a stable id from your data when you have one. That is almost always better than using the array index.
| Choice | When it is fine | When it breaks |
|---|---|---|
| person.id | The id is unique among siblings | Two rows share the same id |
| index (0, 1, 2…) | The list never reorders or inserts in the middle | Sort, delete, or insert - state sticks to the wrong row |
| Math.random() | Never | Every render creates new keys and remounts the rows |
What goes wrong on the page
Wrong keys show up as confusing UI, not always as a crash: labels swap, focus jumps, a checked checkbox moves to another row. The page looks “almost right,” which makes the bug hard to spot.
Open the Debug Library entry linked from Common Mistakes if you want a minimal broken and fixed pair.
What to check when you see a map
- Is there a key on the element returned from map?
- Is the key a stable id from the item?
- If the key is the index, can this list ever reorder?
A that keeps its own input state inside a row is the case that hurts most when keys are wrong. Fix the key before you rewrite the row.
Put the key on the mapped element
The key belongs on the outer element that map returns - the sibling React compares. Putting a key only on something deep inside that row does not identify the sibling. After you change a key, sort or reorder the list and confirm each row’s content stayed with the same person.
Mental model
FOLLOW THE VALUE
Each item becomes a child.
01 / 03Trace the transformation
Live example
The example has two people, Ada and Grace. It logs their names, then renders a list. Each list item uses person.id as its key.
const people = [ // Source data for the list. { id: "ada", name: "Ada" }, // Stable id used as the key. { id: "grace", name: "Grace" }, ]; export default function App() { people.forEach((person) => console.log(person.name)); // Log each person. console.log("key"); // React needs a key on list children. console.log("id"); // Prefer the item id, not the index. console.log("stable"); // The key must stay with the item. return ( <ul> {people.map((person) => ( <li key={person.id}>{person.name}</li> // key identifies this sibling. ))} </ul> ); }
VISUAL FLOW
Try the same flow interactively
map turns each data item into UI. The key tells React which row is which when the list changes.
Click through array → map → keys
ada
Ada
grace
Grace
Start with an array
const people = [{ id: "ada", name: "Ada" }, …];Each object has a stable id and a name. The id will become the key.
Guided challenge
Use the item’s id
The list logs index. Change that log to id so the key matches the person, not the slot.
const people = [ { id: "ada", name: "Ada" }, { id: "grace", name: "Grace" }, ]; export default function App() { people.forEach((person) => console.log(person.name)); // Keep the names. console.log("key"); // Keep the key label. console.log("index"); // Wrong - change this log to id. console.log("stable"); // Keep the stable label. return ( <ul> {people.map((person, index) => ( <li key={index}>{person.name}</li> // Index key - also fix this. ))} </ul> ); }
EXPECTED CONSOLE OUTPUT
Ada Grace key id stable
Hints 0/2
Free sandbox
Make a variation, test an assumption, or break the example on purpose. This draft is saved separately.
const people = [ { id: "ada", name: "Ada" }, { id: "grace", name: "Grace" }, ]; export default function App() { return ( <ul> {people.map((person) => ( <li key={person.id}>{person.name}</li> // Try reordering with this key. ))} </ul> ); }
Quiz
A quick check for understanding. Retry as often as you like; your learning path stays open.
Ready to call this one understood?
Mark this lesson complete to earn 100 XP. You can always revisit it.