Problem is the following:
I have data in form of a list of a few thousand elements. Some of them are duplicates, and there might be the chance of having duplicate keys as well then. Because I have no real "ID" or anything which would give me the opportunity to give all elements their id as unique keys, is it okay to use Math.random()
instead?
As far as I understood, the keys are mainly used by react to differentiate the components. I think as far as I don't really have anything to do with the keys in my code, this should go fine? To ensure that there will be no duplicate number I might as well divide two math randoms with each other to get an almost certainly unique key.
Is this a good practice? Can I use this without having to worry about anything?
Yes. Keys should be stable. If they're not, React can't infer that these items might be the same at all, and has to fully rerender them.
When creating a list in the UI from an array with JSX, you should add a key prop to each child and to any of its' children. React uses the key prop create a relationship between the component and the DOM element. The library uses this relationship to determine whether or not the component should be re-rendered.
Approach: We will be using Uuid V4 in this tutorial. Uuid v4 is a React library or package that creates a universally unique identifier (UUID). It is a 128-bit unique identifier generator. The bits that comprise a UUID v4 are generated randomly and with no inherent logic.
Use the Math. random() function to generate a random number in React, e.g. Math. floor(Math. random() * (max - min + 1)) + min .
Every time a component's key changes React will create a new component instance rather than update the current one, so for performance's sake using Math.random() will be sub-optimal to say the least.
Also if you'll be reordering your list of components in any way, using the index as key will not be helpful either since the React reconciler will be unable to just move around existing DOM nodes associated with the components, instead it will have to re-create the DOM-nodes for each list item, which, once again will have sub-optimal performance.
But to reiterate, this will only be a problem if you will be re-ordering the list, so in your case, if you are sure you will not be reordering your list in any way, you can safely use index as a key.
However if you do intend to reorder the list (or just to be safe) then I would generate unique ids for your entities - if there are no pre-existing unique identifiers you can use.
A quick way to add ids is to just map the list and assign the index to each item when you first receive (or create) the list.
const myItemsWithIds = myItems.map((item, index) => { ...item, myId: index });
This way each item get a unique, static id.
tl;dr How to choose a key for new people finding this answer
If your list item have a unique id (or other unique properties) use that as key
If you can combine properties in your list item to create a unique value, use that combination as key
If none of the above work but you can pinky promise that you will not re-order your list in any way, you can use the array index as key, but you are probably better of adding your own ids to the list items when the list is first received or created (see above)
Just implement the following code in your react component...
constructor( props ) { super( props ); this.keyCount = 0; this.getKey = this.getKey.bind(this); } getKey(){ return this.keyCount++; }
...and call this.getKey()
every time you need a new key like:
key={this.getKey()}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With