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)