Understanding unique keys for array children in React.js

后端 未结 12 1336
时光取名叫无心
时光取名叫无心 2020-11-21 05:45

I\'m building a React component that accepts a JSON data source and creates a sortable table.
Each of the dynamic data rows has a unique key assigned to it but I\'m stil

12条回答
  •  暖寄归人
    2020-11-21 06:41

    I fixed this using Guid for each key like this: Generating Guid:

    guid() {
        return this.s4() + this.s4() + '-' + this.s4() + '-' + this.s4() + '-' +
            this.s4() + '-' + this.s4() + this.s4() + this.s4();
    }
    
    s4() {
        return Math.floor((1 + Math.random()) * 0x10000)
            .toString(16)
            .substring(1);
    }
    

    And then assigning this value to markers:

    {this.state.markers.map(marker => (
                  
              ))}
    

提交回复
热议问题