Lists and Keys in React

Lists and Keys in React

Rendering lists is a very common task in React—users, products, posts, menus, tables, etc. Keys help React identify which items change, are added, or removed.


1. What Are Lists in React?

Lists are created by looping through an array and returning JSX using map().

const numbers = [1, 2, 3, 4];

function NumberList() {
  return (
    <ul>
      {numbers.map(num => (
        <li>{num}</li>
      ))}
    </ul>
  );
}        

2. Using map() Correctly

const users = ["Esa", "Rahim", "Karim"];

{users.map(user => (
  <p>{user}</p>
))}        

  • map() returns a new array
  • Each iteration renders JSX


3. What Are Keys?

A key is a unique identifier used by React to track list items.

❌ Without keys → inefficient rendering

✅ With keys → better performance


4. Adding Keys to List Items

const users = [
  { id: 1, name: "Esa" },
  { id: 2, name: "Rahim" },
];

{users.map(user => (
  <p key={user.id}>{user.name}</p>
))}        

Keys must be:

  • Unique
  • Stable
  • Not index-based (if possible)


5. Why Not Use Index as Key?

{users.map((user, index) => (
  <p key={index}>{user}</p>
))}        

Causes issues when:

  • Items are added or removed
  • List is reordered

Use only if data is static.


6. Rendering Complex Lists

function ProductList({ products }) {
  return (
    <div>
      {products.map(product => (
        <div key={product.id}>
          <h3>{product.name}</h3>
          <p>{product.price}</p>
        </div>
      ))}
    </div>
  );
}        

7. Common Use Cases

  • Product listings
  • User dashboards
  • Navigation menus
  • Tables & grids
  • Blog posts


8. Best Practices

  • Always use unique keys
  • Prefer database IDs
  • Keep JSX clean
  • Split into components

Lists and keys are essential for:

  • Performance
  • Predictable UI updates
  • Clean code structure

Every real-world React app uses this pattern extensively.

To view or add a comment, sign in

More articles by F R.

  • Disagreement is NOT Disrespect.

    In a high-growth environment, a "clash of ideas" is inevitable. If everyone always agrees, someone isn't thinking.

  • Budget Gear for New Developers

    In the world of web development, there is a common misconception that you need a top-of-the-line machine to start…

  • Be the Leader People Remember

    There is an employee somewhere who still thinks of you — just because you were kind to them. As a new leader, you may…

  • The Rise of TypeScript: Why You Should Learn It

    TypeScript is growing very fast in the web development world. What started as an optional tool is now becoming a…

    1 Comment
  • Tech Trends to Watch in 2026

    Technology is changing faster than ever. In 2026, many new tools and systems are becoming part of everyday work and…

  • Why Using AI Without Programming Basics Is Dangerous

    Artificial Intelligence has become a powerful assistant for developers. It can write code, debug errors and accelerate…

  • Event Handling in React

    Event handling in React allows users to interact with your application—clicking buttons, typing input, submitting…

  • Conditional Rendering in React

    Conditional rendering allows you to show or hide UI elements based on conditions. It is essential for authentication…

  • From HTML to React- A Developer’s Quiet Evolution

    I still remember the first time I wrote an HTML file. It was simple.

  • useEffect Hook in React

    The hook is one of the most important concepts in React. It allows you to handle side effects such as: Fetching data…

Explore content categories