DEV Community

Aman Kureshi
Aman Kureshi

Posted on

๐Ÿ—๏ธ React Keys โ€” Why They Matter in Lists

When rendering lists in React using map(), each item needs a unique key. Keys help React identify which items changed, added, or removed.

๐ŸŽฏ Why use keys?
โ€ข Improve rendering performance
โ€ข Prevent unnecessary re-renders
โ€ข Keep UI stable during updates

๐Ÿ”ง Example:

const fruits = ["Apple", "Banana", "Orange"];

function FruitList() {
  return (
    <ul>
      {fruits.map((fruit, index) => (
        <li key={index}>{fruit}</li>
      ))}
    </ul>
  );
}

Enter fullscreen mode Exit fullscreen mode

๐Ÿ“Œ Key points:
โ€ข Keys should be unique among siblings
โ€ข Avoid using index as key (except for static lists)
โ€ข Better to use a unique ID from data

Using proper keys makes your React lists efficient and predictable.

Top comments (0)