Skip to content

List Rendering

Rendering arrays of data with map, the importance of keys, and common list patterns.

What you'll learn

  • Using .map() to transform arrays into JSX elements
  • Why keys are essential and how to choose them
  • Filtering and sorting lists before rendering
  • Common pitfalls with index-based keys

Concept

Rendering Lists with map

In React, you render lists of data using the JavaScript Array.map() method. You call map on your data array and return a JSX element for each item. React then renders each element in order. This is pure JavaScript — there is no special v-for or *ngFor directive. The map call goes inside JSX curly braces: {items.map(item => <li>{item.name}</li>)}.

The Importance of Keys

Every element in a list must have a unique key prop. Keys help React identify which items have changed, been added, or been removed, allowing it to efficiently update the DOM. Without keys, React may unnecessarily re-render or incorrectly reuse DOM nodes, causing subtle bugs. Keys must be unique among siblings — they do not need to be globally unique. The best key is a stable, unique identifier from your data, like a database ID.

Why Not Use Array Index as Key?

Using the array index as a key (key={index}) is tempting but problematic if the list can be reordered, filtered, or items can be inserted at the beginning. When the order changes, the indices stay the same but the data behind them changes, so React may reuse the wrong DOM nodes. This can cause state to get attached to the wrong item or animations to break. Use index keys only for static lists that never change order.

Filtering and Sorting

You can chain array methods before mapping: items.filter(...).sort(...).map(...). However, be mindful of performance — filtering and sorting on every render can be expensive for large lists. Consider memoizing the result with useMemo (which you will learn about later) if the list is large and the source data does not change often.

The key Prop in Depth

Keys are React's identity tracker for list items. When a key changes, React treats the element as new and creates a new DOM node. When a key stays the same across renders, React reuses the existing DOM node and updates only the changed attributes. This is why stable, unique keys from your data (like database IDs) are ideal — they let React minimize DOM operations.

Example

              import { useState } from "react";

function ProductList({ products }) {
  const [filter, setFilter] = useState("");

  // Filter then map — chain array methods for derived data.
  const visibleProducts = products
    .filter(p => p.name.toLowerCase().includes(filter.toLowerCase()))
    .map(product => (
      <li key={product.id} className="product-item">
        <h3>{product.name}</h3>
        <p>Price: ${product.price}</p>
        <span className={product.inStock ? "in-stock" : "out-of-stock"}>
          {product.inStock ? "In Stock" : "Sold Out"}
        </span>
      </li>
    ));

  return (
    <div>
      <input placeholder="Search products..."
        value={filter} onChange={e => setFilter(e.target.value)} />
      <ul>{visibleProducts}</ul>
    </div>
  );
}
            

Each list item has a stable key from product.id (not the array index). The filter narrows the list, and map transforms each product into JSX.

Try it

  • JSON Formatter

    List data usually comes as JSON arrays — format and inspect them.

  • JSON to CSV

    Export your list data to CSV for spreadsheets or reports.

Common mistakes

The mistake

Using the array index as the key for dynamic lists

The fix

Use a stable unique ID from your data (e.g., product.id, user.id). Index keys cause bugs when items are reordered, inserted, or deleted because React reuses the wrong DOM nodes.

The mistake

Forgetting the key prop entirely

The fix

React will warn in the console and may render incorrectly. Always provide a unique key prop to every element in a mapped list. Keys must be unique among siblings.

Practice List Rendering

8 exercises

Practice List Rendering

Look up unfamiliar terms

A beginner-friendly glossary explains jargon in plain English — variables, functions, DOM, Promise, and more.

View glossary

Build real projects

Apply what you learned by building guided projects with starter code and solutions.

View projects

Decode error messages

Plain-English explanations of common errors — what they mean, why they happen, and how to fix them.

View errors