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

后端 未结 7 1686
谎友^
谎友^ 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-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.

提交回复
热议问题