Skip to main content

Command Palette

Search for a command to run...

How to Efficiently Render and Display Lists in React

Why Keys Make a Difference in React List Rendering

Updated
4 min read
How to Efficiently Render and Display Lists in React

Understanding how to render lists is essential in React.js. Most production-grade projects rely on this technique for rendering components dynamically. Common examples include displaying a list of products in an e-commerce application or showing a catalog of movies on Netflix.

React.js, combined with JSX, makes list rendering straightforward with minimal code and complexity. By using the map() method alongside JSX, you can render components efficiently. A component can be defined once and reused for an entire list, resulting in clean, maintainable code. However, there are a few important considerations to keep in mind, which we'll explore in this article.

You'll often need to display multiple similar components from a collection of data. JavaScript array methods allow you to manipulate arrays effectively.

Example: Rendering a List of Products

Let's look at a practical example of rendering a product list:

function ProductList() {
  const products = [
    { id: 1, name: 'Laptop', price: 999 },
    { id: 2, name: 'Smartphone', price: 699 },
    { id: 3, name: 'Headphones', price: 199 }
  ];

  return (
    <div>
      <h2>Our Products</h2>
      <ul>
        {products.map(product => (
          <li key={product.id}>
            {product.name} - ${product.price}
          </li>
        ))}
      </ul>
    </div>
  );
}

In this example, we use map() to iterate over the products array and return a list item for each product. Notice the key prop with a unique identifier—this is crucial for React's rendering optimization, which we'll discuss further in the article.


The Importance of Keys in React Lists

Every time you use map() to render JSX elements, it is required to add a key prop. This key must be unique within that particular list. Without a key, React will display a warning in the console

Adding keys is always a good practice and serves a critical purpose. Keys help React identify which elements in a list have been updated, added, or removed. Without keys, React cannot efficiently distinguish between individual items. When an interaction occurs, React may recreate all the nodes in the list, which can result in lost user input, reset component state, or unexpected behavior. In large and complex codebases, this can also introduce bugs and significantly impact performance.

What Makes a Good Key?

The key must remain consistent and stable over time—it should be the same value that was assigned when the element was initially added. You should not use the array index from map() as a key, even though it might seem convenient. While React will accept index-based keys, this approach can cause problems.

If an element is deleted or the list is reordered, the index of other elements will change. This means the same component could receive a different key, causing React to lose track of which element is which. The result can be incorrect rendering, lost state, or buggy behavior.

Best Practice

Always use a stable, unique identifier from your data source, such as:

  • A database ID (e.g., product.id, user.id)

  • A unique identifier generated when the data is created (e.g., UUID)

  • Any other property that uniquely and consistently identifies each item

// ✅ Good - using stable ID from data
{products.map(product => (
  <ProductCard key={product.id} product={product} />
))}

// ❌ Bad - using array index
{products.map((product, index) => (
  <ProductCard key={index} product={product} />
))}

By following this practice, you ensure React can efficiently update your UI and maintain the correct state for each component in your list.

Note -

When using map(), the return statement is not required if you're using an implicit return. However, if you add curly braces {, then an explicit return statement is required. Otherwise, the function will return undefined and nothing will be rendered.

It is also good practice to use optional chaining when accessing nested values to avoid errors if the data is null or undefined. For example, instead of product.id, use product?.id.

// ✅ Implicit return - no braces, no return needed
{products.map(product => (
  <li key={product?.id}>{product?.name}</li>
))}

// ✅ Explicit return - braces require return statement
{products.map(product => {
  return <li key={product?.id}>{product?.name}</li>;
})}

// ❌ Wrong - braces without return
{products.map(product => {
  <li key={product?.id}>{product?.name}</li>;
})}

Follow this practice this will help to write clean react code.