Common React Mistake: Using Index as Key

⚠️ A Common React Mistake We Make as React/JS Developers. I have found that one small mistake in React can create very weird UI bugs while working on using Index as Key in a List. Let me explain with an example. Check this code for displaying a list - {items.map((item, index) => ( <div key={index}>{item.name}</div> ))} The above piece of code is something we commonly use. It looks fine, but sometimes it can be dangerous - and we often ignore it. Let me explain why. At first, everything looks fine. No errors. But the real problem starts when the list changes - during insertion or deletion. Imagine the list is: 0 - React 1 - Node 2 - Next Now suppose we delete "React" (index 0). Hence, the new list becomes: 0 - Node 1 - Next But here is the problem. React thinks: Item with key 0 still exists. Item with key 1 still exists. Because the keys (0 and 1) are still there. So instead of understanding: "React was removed" React thinks: "React became Node" "Node became Next" It reuses the old components and just changes the data. Result: Wrong component updates. How can we fix it? Using a stable id as the key. {items.map((item) => ( <div key={item.id}>{item.name}</div> ))} Now imagine: id: 101 - React id: 102 - Node id: 103 - Next We delete React (101). React sees: 101 is gone 102 still exists 103 still exists So, it removes that exact component. Share your thoughts in the comment box. #ReactJS #JavaScript #WebDevelopment #FrontendDevelopment #ReactDeveloper #CodingMistakes #SoftwareDevelopment #LearnToCode #Programming

  • text

“This isn’t a React mistake — it’s a misunderstanding of how React works.” Experience isn’t just about years — it’s about understanding fundamentals deeply. Concepts like identity vs position may seem simple, but they directly impact how React handles reconciliation and state. Real growth happens when we go beyond syntax and understand the “why” behind it. map() is not a problem in real-time rendering. Performance issues usually come from unstable keys, large datasets, or unnecessary re-renders — not from iteration itself. "if some one use position as id . They may not understand reconciliation deeply".

For me, I'll say we have two edge cases here. A scenario where there is a possibility of a particular item on the list not being there again due to deletion, and a case where it remains that way. Consider an analytics dashboard. I have a reusable card component, then I mapped through an array of objects that have title, subtext and icon info, and in each object, a response from the api was assigned to the ‘total’. Remember I positioned the card index based on which should come first on the UI. When you map through the array to show your cards, would you consider using the index as the key if ‘id’ doesn't exist considering that the index of each card will never change? Personally, I make use of id always, but there have been scenarios where I use index as well The point is, for items whose position is bound to change, using a unique id identifier is advisable, if it's its counterpart, index is available for you, you don't need to look for an ID if it doesn't exist on the object. An also you can combine index in cases of static items on the DOM e.g “index + name”. I added a reference code and even though I didn't use the index here, using it won't hurt performance. That's just a personal opinion and thanks for sharing.

  • No alternative text description for this image

If we don’t have Id then option A is better otherwise option B

One thing I’ve really come to appreciate as a senior developer is how important the key prop is in React. It may seem small, but it’s what helps React’s diffing algorithm understand what actually changed. Using stable, unique keys makes re-renders predictable and saves you from subtle UI bugs. So B is always the right way to do it.

Using stable id instead of index also helps React when using D&D or similar functionality of changing order of items frequently

item.id, we can access the same id across whole global state. it's better to filter, find and other operation we need as for the application

See more comments

To view or add a comment, sign in

Explore content categories