🚨 I used index as key in React… …and spent HOURS debugging something that made no sense. Everything looked fine at first. Until I: → deleted an item → reordered the list 💥 Suddenly: ❌ Wrong data was displayed ❌ UI behaved randomly ❌ Bugs I couldn’t explain I kept checking my logic. Turns out… the bug wasn’t my logic. 👉 It was THIS: {data.map((item, index) => ( <div key={index}>{item.name}</div> ))}💡 Problem: React uses keys to track identity. When you use index: → identity = position ❌ → not the actual item So when the list changes… React gets confused 😵 ✅ Fix: {data.map((item) => ( <div key={item.id}>{item.name}</div> ))}👉 Stable key = stable UI 💡 Lesson: If your UI feels “random”… check your keys before your logic. Follow me for more such tips ✍️ 👨💻 #ReactJS #Frontend #JavaScript #SoftwareEngineering
In a large api fetch, some item.id might be the same, so some cases you could even do some additions with the id
Aman Yadav yes key matters it's help to virtual dom for differentiation so it can update or re-render that part cool keep it with this snippets
great work Aman Yadav keep it up 🤞 all the best for future
Aman Yadav Well explained! Keys are all about identity, not position, using index feels right… until React decides to surprise you 😅. Stable IDs are the best choice here
Great approach