When giving unique keys to components, is it okay to use Math.random() for generating those keys?

后端 未结 7 1633
谎友^
谎友^ 2020-12-04 23:44

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 ke

相关标签:
7条回答
  • 2020-12-04 23:59

    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

    1. If your list item have a unique id (or other unique properties) use that as key

    2. If you can combine properties in your list item to create a unique value, use that combination as key

    3. 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)

    0 讨论(0)
  • 2020-12-05 00:09

    I think its not correct to give Math.random() for components keys, reason is when you generate random number it is not guaranteed not to get same number again. It is very much possible that same random number is generated again while rendering component, So that time it will fail.

    Some people will argue that if random number range is more it is very less probable that number will not be generated again. Yes correct but you code can generate warning any time.

    One of the quick way is to use new Date() which will be unique.

    0 讨论(0)
  • 2020-12-05 00:14

    Keys should be stable, predictable, and unique. Unstable keys (like those produced by Math.random()) will cause many component instances and DOM nodes to be unnecessarily recreated, which can cause performance degradation and lost state in child components.

    You should add a key to each child as well as each element inside children.

    This way React can handle the minimal DOM change.

    Please go through the https://reactjs.org/docs/reconciliation.html#recursing-on-children. Here you will get best explanation with example of code.

    0 讨论(0)
  • 2020-12-05 00:15

    From react documentation:

    Keys should be stable, predictable, and unique. Unstable keys (like those produced by Math.random()) will cause many component instances and DOM nodes to be unnecessarily recreated, which can cause performance degradation and lost state in child components.

    0 讨论(0)
  • 2020-12-05 00:18

    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()}
    
    0 讨论(0)
  • 2020-12-05 00:18

    Keys should be stable, predictable, and unique. Unstable keys (like those produced by Math.random()) will cause many component instances and DOM nodes to be unnecessarily recreated, which can cause performance degradation and lost state in child components.

    https://facebook.github.io/react/docs/reconciliation.html

    0 讨论(0)
提交回复
热议问题