← Back to Chapters

Rendering Lists with map()

⚛️ Rendering Lists with map()

? Quick Overview

In React, the JavaScript map() method is used to transform arrays into lists of JSX elements. This pattern is essential for rendering dynamic data such as menus, tables, cards, and reusable components.

By combining map() with proper key props, you can build efficient and maintainable UIs that update smoothly when data changes.

? Core Pattern: array.map(item => JSX)

? Key Concepts

  • map() for JSX: Convert each array element into a React element or component.
  • Keys: The key prop uniquely identifies each rendered item for React.
  • Rendering Objects: Map over arrays of objects and display selected properties.
  • Component Lists: Use map() to render custom components instead of plain HTML.
  • Conditional Logic: Combine filter(), conditions, and map() for smart lists.
  • Nested Lists: Use nested map() calls for hierarchical data structures.

? Syntax & Theory

The JavaScript map() method creates a new array by applying a callback function to each element:

General form:
newArray = array.map((item, index) => { /* return something */ });

In React, the callback usually returns JSX. Each top-level JSX element in the list should have a key prop so React can track which items were added, removed, or reordered.

? Basic Example: Array of Strings

Suppose you have an array of names and want to display them as a list:

? View Code Example
// Render a simple list of names using map()
function NameList() {
const names = ["Alice", "Bob", "Charlie"];

return (
<ul>
{names.map((name) => (
<li key={name}>{name}</li>
))}
</ul>
);
}

? What This Renders

The browser will display:

  • Alice
  • Bob
  • Charlie

Each item in the names array becomes an <li> element. The key prop uses the name itself, which is fine here because the values are unique and stable.

? Rendering Objects

When the array contains objects, you can still use map() and pick which properties to display.

? View Code Example
// Render a list of products with name and price
function ProductList() {
const products = [
{ id: 1, name: "Laptop", price: 60000 },
{ id: 2, name: "Phone", price: 25000 },
{ id: 3, name: "Tablet", price: 30000 }
];

return (
<ul>
{products.map((p) => (
<li key={p.id}>{p.name} - ₹{p.price}</li>
))}
</ul>
);
}

? Explanation

Here, each product is an object with id, name, and price. The id is used as a unique key, which is a good real-world practice.

? Rendering a List of Components

Instead of rendering plain list items, you can render custom components for cleaner and reusable code:

? View Code Example
// Use a reusable Product component inside a mapped list
function Product({ name, price }) {
return <li>{name} — ₹{price}</li>;
}

function ProductList() {
const items = [
{ id: 1, name: "Keyboard", price: 1500 },
{ id: 2, name: "Mouse", price: 800 },
{ id: 3, name: "Monitor", price: 12000 }
];

return (
<ul>
{items.map((item) => (
<Product key={item.id} name={item.name} price={item.price} />
))}
</ul>
);
}

? What This Renders

The output is still a list of products, but the UI logic is split into a dedicated Product component. This makes your list easier to style and reuse in other parts of the app.

? Conditional Rendering with map()

You can combine filter() and map() (or inline conditions) to show only some items.

? View Code Example
// Filter even numbers, then render them as a list
function FilteredList() {
const numbers = [1, 2, 3, 4, 5, 6];

return (
<ul>
{numbers
.filter((n) => n % 2 === 0)
.map((n) => (
<li key={n}>Even: {n}</li>
))}
</ul>
);
}

? Result

Only the even numbers (2, 4, 6) are rendered. This pattern is very common when building filtered lists, search results, or dashboards.

⚙️ Nested Lists with map()

For hierarchical data (like categories and sub-items), you can nest map() calls:

? View Code Example
// Render categories and their nested items
function CategoryList() {
const categories = [
{ id: 1, name: "Electronics", items: ["Phone", "Laptop", "Camera"] },
{ id: 2, name: "Clothing", items: ["Shirt", "Jeans"] }
];

return (
<div>
{categories.map((cat) => (
<div key={cat.id}>
<h5>{cat.name}</h5>
<ul>
{cat.items.map((item) => (
<li key={item}>{item}</li>
))}
</ul>
</div>
))}
</div>
);
}

?️ How It Works

The outer map() loops through categories, and the inner map() loops through each category's items. Both the category and each item have their own key values.

? Common Use Cases

  • Rendering menus, navigation bars, and sidebars from configuration arrays.
  • Displaying fetched API data such as products, posts, or comments.
  • Generating UI elements like cards, tables, and grids from data.
  • Building dashboards with dynamic widgets and stats blocks.

? Tips & Best Practices

  • Always provide a unique, stable key for each item in a list (IDs are ideal).
  • Avoid using the array index as the key for dynamic lists that can be reordered.
  • Extract list items into reusable components to keep code clean and organized.
  • Use filter() or sort() before map() for custom ordering and filtering.
  • For nested data, ensure both parent and child items have unique keys at their own level.
  • If you need to return multiple elements per item, wrap them with a fragment <>...</>.
  • Keep your map() callbacks small; move complex logic into helper functions or components.

? Try It Yourself

  1. Create a StudentList component to display a list of student names using map().
  2. Render a list of products with their prices using map() and proper key props.
  3. Build a nested list showing categories and sub-items similar to the CategoryList example.
  4. Add a filter to display only items matching a specific condition (e.g., price > 1000).
  5. Refactor one of your lists to use a separate child component for each item.

Goal: Master rendering dynamic lists in React using map() — including arrays, objects, nested data, conditional rendering, and reusable components.