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)