DEV Community

Aman Kureshi
Aman Kureshi

Posted on

🎯 Why You Should Avoid Using Index as a Key in React

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>
Enter fullscreen mode Exit fullscreen mode

✨ Rule:
Index as key = only safe when list never changes.

Better keys β†’ More stable UI. πŸš€

Top comments (0)