React Key Mistakes: Avoid Using Index as Key

Most React developers use keys wrong in lists. And it silently breaks their app. 😅 This is what everyone does: // ❌ Using index as key {users.map((user, index) => ( <UserCard key={index} user={user} /> ))} Looks fine. Works fine. Until it doesn't. The problem: If you add/remove/reorder items — React uses the index to track components. Index changes → React thinks it's a different component → Wrong component gets updated → Bugs that are impossible to debug. 💀 Real example: // You have: [Alice, Bob, Charlie] // You delete Alice // Now: [Bob, Charlie] // Index 0 was Alice, now it's Bob // React reuses Alice's DOM node for Bob // State gets mixed up! // ✅ Always use unique ID as key {users.map((user) => ( <UserCard key={user.id} user={user} /> ))} Rule I follow: → Never use index as key if list can change → Always use unique stable ID → Only use index for static lists that never change This one mistake caused a 2 hour debugging session for me. 😅 Are you using index as key? Be honest! 👇 #ReactJS #JavaScript #Frontend #WebDevelopment #ReactTips #Debugging

  • graphical user interface

Actually, its perfectly fine to use the index as key as long that array is not gonna change (you wont add, remove or edit any of the rows).

Like
Reply

best practice is key=`${index}-${user.id}`

Like
Reply
See more comments

To view or add a comment, sign in

Explore content categories