DEV Community

Discussion on: Bad ReactJs practices to avoid

Collapse
 
jurajuki profile image
Juraj Pavlović • Edited

Good question! One major thing to avoid alongside index is random keys such as uuid. A proper key should be a unique identifier from the data object you are passing. Check out the following example: Keys Example

Collapse
 
reotech profile image
Reotech

Thank you for this

Collapse
 
jamesthomson profile image
James Thomson

One major thing to avoid alongside index is random keys such as uuid
All you need to avoid is setting the key an each render. It's perfectly fine to use a uuid if you define it as part of the object being consumed and that uuid persists.

Thread Thread
 
jurajuki profile image
Juraj Pavlović

Indeed, if the uuid is a part of the object, then of course it is fine to use it. That way it is not a random key like I've mentioned.

Collapse
 
multiwebinc profile image
Mike Robinson • Edited

Instead of uuid, you could use this function that just returns an incrementing integer (guaranteed to be unique and no external dependencies):

var getNextKey = (function(n) {
  return () => ++n;
}(0));
Enter fullscreen mode Exit fullscreen mode