Sub topic 2
Turning arrays into elements with map
This chapter assumes you know JavaScript arrays, objects and arrow functions, can write React function components with props and JSX, and have used useState. Here you will learn how map turns data into elements, which is the core of list rendering in React.
Expressions inside JSX
Curly braces in JSX let you embed any JavaScript expression. An expression is code that produces a value, such as user.name, a + b, or a function call. Statements like if and for do not produce a value, so they cannot go inside braces.
An array of elements is also a valid value. React renders each element in the array in order. This is why map fits so well here: it takes an array of data and returns a new array of elements.
Mapping data to elements
Array.prototype.map calls a function on every item and collects the return values into a new array. It does not change the original array. In React you return a JSX element from that function.
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>
);
}The expression people.map(...) evaluates to an array of two <li> elements. React places them inside the <ul>. Each <li> has a key prop, which is the next topic in this tutorial. For now, note that the key comes from person.id, a value that identifies each person.
Common mistakes
Most errors with map in JSX come from a few habits.
- Using braces instead of parentheses around the arrow body.
person => { <li>...</li> }opens a function body that returns nothing, so the list is empty. Useperson => (<li>...</li>)or write an explicitreturn. - Forgetting to return from a block body. If you use braces for the arrow function, add
return. - Using
forEach. It returnsundefined, so there is nothing for React to render. Usemap. - Omitting the key. React logs a warning in development. The next chapters explain why keys matter.
// Wrong: the braces make a function body, and nothing is returned
{people.map(person => {
<li key={person.id}>{person.name}</li>
})}
// Right: explicit return in a block body
{people.map(person => {
return <li key={person.id}>{person.name}</li>;
})}Mapping into components
The function you pass to map can return any JSX, including your own components. Pass the item's fields as props.
function Person({ name }) {
return <li>{name}</li>;
}
function PeopleList({ people }) {
return (
<ul>
{people.map(person => (
<Person key={person.id} name={person.name} />
))}
</ul>
);
}The key stays on the element returned from map, which here is <Person />, not on the <li> inside the component. React reads the key from the outermost element in the mapped array.
Exercise
A teammate writes {items.map(item => { <li key={item.id}>{item.name}</li> })} and the list renders nothing. What is wrong, and how do you fix it?
Show answer
The arrow function uses braces, so it has a function body with no return statement, and it returns undefined for every item. Fix it by replacing the braces with parentheses (item => (<li ...>...</li>)) or by adding return before the <li>.