DEV Community

Discussion on: You shouldn't use an index as key of the element

Collapse
 
sanderdebr profile image
sanderdebr

Good article, you could also use a package like uuid() (npmjs.com/package/uuid) to generate a random unique key for you. This way you also avoid re-rendering if your amount of rows changes in this case, but it will create another dependency

Collapse
 
rajnishkatharotiya profile image
Rajnish Katharotiya

Solution can work, but same it will add dependency. But i think if we have any value unique in our array itself then we should just use it as key.

Collapse
 
bytebodger profile image
Adam Nathaniel Davis

That's a dangerous pattern to follow. I've been down that path, years ago. At worst, it can spawn some nasty bugs. At best, it's needlessly inefficient.

stackoverflow.com/questions/513719...

Collapse
 
sanderdebr profile image
sanderdebr

That is interesting, I think that in your example he is generating the unique key inside the map function, which will cause re-generating the keys for every re-render. If you put the unique key outside the loop function, e.g. as an object property, it should be fine:

medium.com/@robinpokorny/index-as-...