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 (1)

Some comments may only be visible to logged-in visitors. Sign in to view all comments.