Skip to content
Core ConceptsChapter 11 / 37Beginner4 min read

Lists & Keys

Learn how to render arrays in React with map, why the key prop matters, and what goes wrong when you use the array index as a key.

On this page

1. What are lists and keys?

Data often lives in an array: products, users, comments. map() turns each item into JSX. A key is like a small name tag that tells React which JSX belongs to which item.

2. Why does it matter?

No key or a wrong keyA stable unique key
React identifies items only by positionReact identifies each item by its id
When an item is deleted, state moves to the wrong rowState stays with the right item
A key warning in the consoleNo warnings
More DOM workOnly the changed item is updated

3. Where is it used?

Use caseWhat the key should be
Products gridproduct.id
Chat messagesmessage.id
Todo listcrypto.randomUUID() when the todo is created
Table rowsThe database primary key
Static menuA unique href or slug

4. How does it work?

Rendering and updating a list
An array of data
map() turns each item into JSX
Each JSX element gets a unique key
On the next render, React compares the keys
New items are added, removed ones are deleted, the rest stay the same

A key only needs to be unique among siblings, not across the whole app. The key is not passed to the component as a prop, it is only for React.

5. Code example

Basic list

jsx
const products = [
  { id: "p1", name: "Keyboard", price: 4500 },
  { id: "p2", name: "Mouse", price: 1800 },
  { id: "p3", name: "Monitor", price: 32000 },
];

export default function ProductList() {
  return (
    <ul>
      {products.map((product) => (
        <li key={product.id}>
          {product.name}: Rs. {product.price}
        </li>
      ))}
    </ul>
  );
}

Filter, then map

jsx
function CheapProducts({ products }) {
  const cheap = products.filter((p) => p.price < 5000);

  if (cheap.length === 0) {
    return <p>No cheap products found.</p>;
  }

  return (
    <ul>
      {cheap.map((p) => (
        <ProductRow key={p.id} product={p} />
      ))}
    </ul>
  );
}

How does the code work?

PartMeaning
products.map(...)Each object becomes one li
key={product.id}A stable unique id that comes from the data
filter then mapFilter first, then render
<ProductRow key=... />The key goes on the component, not on the li inside it
Empty checkShows a message when the list is empty

6. Common mistakes

Mistake: Using the array index as a key when the list can be reordered, deleted from or inserted into

Why? When an item is removed, the indexes shift, and React gives the old state and input value to the wrong item.

Better: Use the data's own id. The index is only fine when the list never changes.

Mistake: Creating keys with Math.random() or randomUUID() during render

Why? Every render produces a new key, so React treats every item as new and rebuilds the DOM and state.

Better: Generate the id once when the data is created and store it with the item.

Mistake: Putting the key on an element inside the component

Why? React needs the key on the outermost element returned from map. Putting it inside causes a warning.

Better: Put the key where the map callback returns JSX.

Interview tip

Answer "Why is index as key bad?" with an example: in a list of inputs, delete an item in the middle and the typed text shifts to the wrong row. This is a reconciliation problem.

7. What to learn before and after

Final quick revision

PointDetail
Render a listarray.map()
keyUnique and stable among siblings
Best keyThe id from your data
Index as keyOnly for static lists
Random keyNever