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>
);
}
π 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)