React Rendering Lists
Table of Contents + โ
In the last lesson you learned about React Rendering Empty States. This lesson is about the opposite case: turning an array of data into a list on screen without typing one element by hand.
๐ค Why do we need to render lists?
Real apps are full of lists, and the data almost always lives in an array.
- YouTube shows a list of videos. WhatsApp shows a list of chats. Amazon shows a list of products.
- That data sits in an array, like
["Riya", "Alex", "Sam"]or a list of objects. - You canโt type one
<li>per name by hand. The list changes all the time, and it can have ten items or ten thousand. - So you need a way to say โtake this array and give me one element for each itemโ. That is rendering lists.
๐บ๏ธ Turning an array into elements with map
The tool for this is JavaScriptโs map method, and it fits React perfectly.
mapwalks through every item in the array.- For each item, you give it back something. Here, you give back a piece of JSX.
- It returns a brand-new array, but now full of elements instead of plain data.
- React is happy to take an array of elements and put them all on screen.
Here we take an array of names and turn it into an array of list items.
const names = ["Riya", "Alex", "Sam"];
const items = names.map((name) => <li>{name}</li>);// items is now: [<li>Riya</li>, <li>Alex</li>, <li>Sam</li>]So map did the boring part for you. One name went in, one <li> came out, for every item in the list.
map returns a value, that is the whole trick
map is special because it returns a new array. A for loop does not return anything, it just runs. That is why map works so well with React. You can use its result directly as the thing to render.
๐ Rendering a list of strings
Now letโs do it inside a real component. The map goes right inside the JSX, between curly braces.
- You write
{to drop into JavaScript inside your JSX. - Then
names.map(...)runs and gives back the array of<li>elements. - React reads that array and shows every element in order.
- This works because
mapreturns a value, and JSX braces accept any value.
Here a component takes an array of names and renders each one as a list item.
function NameList() { const names = ["Riya", "Alex", "Sam"];
return ( <ul> {names.map((name) => ( <li key={name}>{name}</li> ))} </ul> );}So the <ul> holds the list, and the map inside fills it with one <li> per name. Change the array, and the list on screen changes with it.
๐ Why map and not a for loop
People coming from other languages often reach for a for loop here, but inside JSX that does not work.
- JSX braces only accept an expression, which is something that produces a value.
mapis an expression. It produces a value, a new array of elements. So it fits inside the braces.- A
forloop is a statement. It does not produce a value, it just runs steps. So it cannot go inside the braces.
Here is the wrong way next to the right way, so the difference is clear.
// โ Wrong - a for loop is a statement, it returns nothing, JSX braces reject it<ul> { for (let i = 0; i < names.length; i++) { <li>{names[i]}</li> } }</ul>
// โ
Right - map is an expression, it returns an array of elements<ul> {names.map((name) => <li key={name}>{name}</li>)}</ul>So remember this. Inside JSX you need something that gives back a value, and map does exactly that.
๐ Mapping objects to components
Most real data is not plain strings. It is a list of objects, and you map those the same way.
- Each item is an object, like
{ id: 1, name: "Riya", role: "Designer" }. - Inside
mapyou read the fields you need, likeuser.nameanduser.role. - You return a bigger piece of JSX, like a card, instead of a single line.
Here we turn an array of user objects into a list of small cards.
function UserCards() { const users = [ { id: 1, name: "Riya", role: "Designer" }, { id: 2, name: "Alex", role: "Developer" }, { id: 3, name: "Sam", role: "Manager" }, ];
return ( <div> {users.map((user) => ( <div key={user.id} className="card"> <h3>{user.name}</h3> <p>{user.role}</p> </div> ))} </div> );}So each object becomes its own card, with its name and role filled in. Same map, just a bigger element coming out each time.
๐ Each element needs a key
You may have noticed the key on every element above. React asks for it whenever you render a list.
- A
keyis a special prop you put on each element in a mapped list. - It gives React a stable id for each item, so React can tell them apart when the list changes.
- Without a key, React shows a warning, and updates to the list can behave in odd ways.
- The key should be something unique and steady, like an
idfrom your data. Not the array index if you can avoid it.
Here is the key sitting on each item, using a unique id from the data.
{users.map((user) => ( <div key={user.id} className="card"> <h3>{user.name}</h3> </div>))}So for now, just always add a key with a unique value when you map. We will go deep on why keys matter, and how to pick a good one, in the very next lesson.
React can render an array directly
You donโt have to wrap the result in anything special. React happily takes an array of elements and renders each one. That is exactly why {items.map(...)} works straight inside your JSX.
๐งฉ What Youโve Learned
- โ Lists in React come from arrays of data, and you turn them into elements to show on screen
- โ
The
mapmethod walks the array and returns a new array of JSX elements, one per item - โ
You write the map inside JSX braces, like
{items.map((item) => <li>{item}</li>)} - โ
mapworks inside JSX because it is an expression that returns a value, unlike aforloop - โ Map an array of objects the same way, returning a bigger element like a card for each one
- โ
Every element in a mapped list needs a unique
keyprop so React can tell items apart
Check Your Knowledge
Test what you learned. Pick an answer for each question, then click Check.
- 1
Why do you use the map method to render a list in React?
Why: map walks every item and returns a new array of elements. React takes that array of elements and renders each one on screen.
- 2
Why can't you use a for loop inside JSX curly braces to build a list?
Why: JSX braces only accept an expression, which is something that produces a value. map returns an array of elements, so it fits. A for loop is a statement and returns nothing, so it does not.
- 3
What does {names.map((name) => <li>{name}</li>)} produce?
Why: map returns a new array where each name has been turned into an <li>{name}</li> element. React then renders that array of elements.
- 4
Why does each element in a mapped list need a key prop?
Why: A key gives React a stable id for each item. That lets React track which item is which when the list updates. Without it, React warns and updates can behave oddly.
๐ Whatโs Next?
You can now turn any array into a list of elements with map, and youโve met the key prop along the way. Next youโll learn exactly why keys matter, how React uses them, and how to pick a good one.