Mapping Nested Arrays
You'll learn how to render lists inside lists by nesting .map() calls and managing keys at every level.
Sometimes your data isn't flat—imagine a list of categories, each containing a list of products. To render this structure, you'll nest one .map() inside another.
The Core Pattern
When you have an array of objects, and each object contains another array, you call .map() on the outer array, then .map() again on each inner array:
function CategoryList({ categories }) {
return (
<div>
{categories.map((category) => (
<div key={category.id}>
<h2>{category.name}</h2>
<ul>
{category.items.map((item) => (
<li key={item.id}>{item.name}</li>
))}
</ul>
</div>
))}
</div>
);
}
Keys at Every Level
Critical rule: Every .map() needs a key prop on its direct child element. The outer map puts a key on the outer <div>, and the inner map puts a key on each <li>. These keys are scoped to their own list—they don't need to be globally unique, just unique within their sibling group.
Think of It Like Filing Cabinets
The outer map creates drawers (categories), and the inner map creates folders inside each drawer (items). React needs labels on the drawers and labels on the folders to track changes efficiently.
Common Mistake
Forgetting the key on the inner map's element. Both levels need keys, or React will warn you and performance suffers.
Key Takeaway: Nested arrays need nested .map() calls, and each .map() must provide a unique key prop at its own level—treat each list independently.