Rendering a List

What this is. React renders arrays of elements. So to show a list, turn your data into an array of elements with map.

Why map and not forEach. map returns a new array. forEach returns nothing, so it renders nothing. This is one of those bugs where the code looks entirely reasonable and the screen is simply empty.

Try it

Change map to forEach. The list disappears, with no error to tell you why.

loading editor…

loading React…

✦ The arrow must return

books.map(b => <li>...</li>) returns each element. books.map(b => { <li>...</li> }) returns undefined, because braces open a function body rather than wrapping an expression. If you use braces, you must write return.

Mark this lesson complete

Signed in, your progress follows you to every device.

Next: Keys, and Why Index Is Usually Wrong →
← Back