React uses keys to track list items, but using the array index can create UI bugs.
π Problem with index as key:
β’ Wrong item gets updated
β’ Animations break
β’ Items reorder incorrectly
π Better Approach:
Always use a unique ID from your data:
<li key={item.id}>{item.name}</li>
β¨ Rule:
Index as key = only safe when list never changes.
Better keys β More stable UI. π
Top comments (0)