What You'll Learn
You'll discover the essential pattern for turning arrays of data into lists of UI components: array.map() combined with JSX.
The Core Pattern
In React, when you have an array of items and need to display each one, you use JavaScript's .map() method. The pattern looks like this:
const items = ['Apple', 'Banana', 'Cherry'];
function FruitList() {
return (
<ul>
{items.map((fruit) => (
<li>{fruit}</li>
))}
</ul>
);
}
How It Works
Think of .map() as a transformation machine. You feed it an array of data (strings, numbers, objects), and it spits out an array of JSX elements. React then renders all those elements to the screen.
The pattern has three parts:
- The array you want to render (
items) - The
.map()call that loops through each element - The JSX you return for each item
Here's a more realistic example with objects:
const users = [
{ name: 'Alice', age: 28 },
{ name: 'Bob', age: 32 },
{ name: 'Carol', age: 25 }
];
function UserList() {
return (
<div>
{users.map((user) => (
<div>
<p>{user.name} is {user.age} years old</p>
</div>
))}
</div>
);
}
Why This Pattern?
Arrays don't render directly in JSX. You can't just write {items} and expect a formatted list. The .map() pattern transforms your data array into an array of JSX elements—something React can render.
Key Takeaway: The pattern array.map((item) => <Component />) is how you transform data arrays into UI lists in React. Every list rendering starts here.