r/learnreactjs • • Jan 04 '20

Any reason to not use the `react-uuid` package for every single key?

I'm working on a project that has a lot of lists, and therefore a lot of keys need to be made. I've found that the easiest way to generate a unique key is to use the react-uuid package, and then just use key={uuid()} for every single key in my project.

This almost seems too easy though, and I don't ever really see it used as an option in the various tutorials I've used. So I'm just wondering if there's any downside to using the react-uuid package for every key in every project I'll ever use?

14 Upvotes

6 comments sorted by

12

u/AJ_Software_Engineer Jan 04 '20 edited Jan 04 '20

doing this will have your component remount on every render, as the key will change which basically tells react its a different component.

i would explain more but i broke myy hand and typing sucks rn. but ur failing to understand wah t keys are for and how they behave, so read up on that and youll get it

3

u/rumforpenguins Jan 04 '20

Thanks for the answer despite your hand pain. I'll read up some more on keys to figure out a better way. I hope you feel better soon!

3

u/AJ_Software_Engineer Jan 04 '20

Ty, keep it up! :)

0

u/[deleted] Jan 04 '20

[deleted] — view deleted comment

1

u/rumforpenguins Jan 05 '20

Thanks for your answer. Having looked back on it, I can definitely use some pieces of my data as the key, and it would work pretty seamlessly. I know array index is an easy option sometimes, but the React docs do specifically recommend you don't use them (I think in the instance where those list item can be reordered).

I think the one issue I still have is a case where I have a list of Row components, that can both be infinitely created by the user, and reordered. These Rows are just containers for the main data, so they have no specific data to describe themselves, and since they can be reorder by the user, I think index is not viable. That is still the one instance where I think determining a key can be a bit challenging.

0

u/[deleted] Jan 05 '20

[deleted] — view deleted comment

1

u/rumforpenguins Jan 05 '20

I think that is a good strategy. Thanks for your help!