Sub topic 3
Rendering your first list
This chapter assumes you know JavaScript arrays, objects, arrow functions, and map. It also assumes you can write a React function component with props and JSX, and that you have used useState. Here you will combine those pieces to turn an array of data into a list on the screen.
From data to elements
React renders whatever JavaScript expression you place inside curly braces in JSX. An array of elements is a valid expression, and React renders each element in order. So the task is to turn your data array into an array of elements.
Array.prototype.map does this. It calls a function on each item and returns a new array of the results. If each result is a JSX element, you get an array of elements.
The first list
const people = [
{ id: 1, name: 'Ada' },
{ id: 2, name: 'Grace' },
];
function PeopleList() {
return (
<ul>
{people.map(person => (
<li key={person.id}>{person.name}</li>
))}
</ul>
);
}Follow what happens:
people.map(...)runs once for each object in the array.- The arrow function receives one
personand returns an<li>element. mapcollects those elements into a new array.- React renders the array inside the
<ul>.
The arrow function uses parentheses around the JSX, not braces. Parentheses return the JSX expression directly. If you wrote braces, you would need an explicit return. Forgetting it is a common error. The list then renders nothing, because map produces an array of undefined values.
The key prop
Each <li> has a key prop. A key is a special prop that tells React which array item an element belongs to. When the array changes, React compares old and new keys to work out which elements were added, removed, or moved. Without keys, React cannot match them reliably.
For now, follow one rule: use a value that is unique within the list and belongs to the data, such as person.id. Put the key on the outermost element returned by the map callback. The next chapter explains why keys matter and how to choose them.
If you leave the key out, React logs a warning in the browser console. It still renders the list, but you lose reliable behavior when the data changes.
Where the key goes
The key belongs in the map callback, on the element you create there. It does not belong inside a component you extract later. This version is wrong:
function PersonItem({ person }) {
// The key here has no effect on the list
return <li key={person.id}>{person.name}</li>;
}
function PeopleList() {
return (
<ul>
{people.map(person => (
<PersonItem person={person} />
))}
</ul>
);
}React reads the key from the element in the array, which is <PersonItem />. Move the key there:
<PersonItem key={person.id} person={person} />Also note that key is not passed to the component as a prop. PersonItem cannot read props.key. If it needs the id, pass it under another name.
Common errors
| Symptom | Likely cause |
|---|---|
| Nothing renders | The callback uses braces with no `return` |
| Console warning about keys | No `key` on the element returned by `map` |
| Key warning persists after extracting a component | The key is inside the component, not on its usage in `map` |
| Text such as `[object Object]` or an error about objects | You rendered an object directly instead of a field like `person.name` |
Exercise
This component renders nothing and no error appears. Why? {people.map(person => { <li key={person.id}>{person.name}</li> })}
Show answer
The arrow function body uses braces, so it needs an explicit return. Without one, each call returns undefined. Either add return or replace the braces with parentheses to return the JSX directly.
Exercise
You extract PersonItem and render <PersonItem person={person} /> inside map. Where must the key go?
Show answer
On the <PersonItem /> element inside map, for example <PersonItem key={person.id} person={person} />. React reads the key from the element in the array.