简体   繁体   English

警告:数组或迭代器中的每个孩子都应该有一个唯一的“键”道具

[英]Warning: Each Child in an Array or Iterator Should Have a Unique “Key” Prop

When i map an array with Picker item i got the following warning:当我使用 Picker 项目映射数组时,我收到以下警告:

Each child in an array should have a unique "key" prop.数组中的每个孩子都应该有一个唯一的“键”道具。

Here is my code这是我的代码

var locationArray = [{"name":"canada","id":"2"},{"name":"sweden","id":"3"}];

var Locations = locationArray.map(function(result) {
return <Picker.Item label={result.name} value={result.id} /> ;
});

return (
    <ScrollView contentContainerStyle={styles.contentContainer}>
        <View style={styles.container}>
            <Picker selectedValue={this.state.location} onValueChange={(location) => this.setState({location:location})} style={[styles.picker, {color: '#2E3740'}]}>
                {Locations}
            </Picker>
        </View>
    </ScrollView>
);

Better performance更好的性能

This is a suggestions of reactjs to improve the rendering performance.这是 reactjs 提升渲染性能的建议。 By providing a unique key for each dynamically created element, it is possibile to minimize possible DOM changes.通过为每个动态创建的元素提供唯一的键,可以最大限度地减少可能的 DOM 更改。

Update your code更新您的代码

Use the id of each element as a unique key.使用每个元素的 id 作为唯一键。 Alternatively would it be possible to use the index of each element.或者,是否可以使用每个元素的索引。

var locationArray = [{"name":"canada","id":"2"},{"name":"sweden","id":"3"}];

var Locations = locationArray.map(function(result) {
return <Picker.Item key={result.id} label={result.name} value={result.id} /> ;
});

return (
    <ScrollView contentContainerStyle={styles.contentContainer}>
        <View style={styles.container}>
            <Picker selectedValue={this.state.location} onValueChange={(location) => this.setState({location:location})} style={[styles.picker, {color: '#2E3740'}]}>
                {Locations}
            </Picker>
        </View>
    </ScrollView>
);

Even I faced the same warning message and was able to overcome it by adding a unique key to the return object like in the below code.甚至我也遇到了同样的警告消息,并且能够通过向返回对象添加一个唯一键来克服它,如下面的代码所示。

Code with warning message带有警告信息的代码

 getBatchList() {
    return this.state.availableBatch.map((data) => {
        return (
            <RadioButton value={data.label}>
                <Text style={styles.batchText}>{data.label}</Text>
            </RadioButton>   
        )
    })
 }

Code without warning message没有警告信息的代码

 getBatchList() {
    //LIKE THIS
    var keyId=1;
    return this.state.availableBatch.map((data) => {
        return (
            <RadioButton value={data.label} key={keyId}>
                <Text style={styles.batchText}>{data.label}</Text>
            </RadioButton>   
        )
        keyId = keyId+1;
    })

}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 警告:数组或迭代器中的每个子代都应具有唯一的“键”道具 - Warning :Each child in an array or iterator should have a unique “key” prop 警告:数组或迭代器中的每个子代都应具有唯一的“键”道具。 li中已添加的关键道具 - Warning: Each child in an array or iterator should have a unique “key” prop.; key prop already added in li 持久警告:“即使已经设置了密钥,数组或迭代器中的每个子节点都应该具有唯一的&#39;密钥&#39;prop” - Persistent Warning: “Each child in an array or iterator should have a unique 'key' prop” even if key is already set 警告:数组或迭代器中的每个子代均应具有唯一的“键”道具。 检查`单位`的渲染方法 - Warning: Each child in an array or iterator should have a unique “key” prop. Check the render method of `Units` 警告:数组或迭代器中的每个子代均应具有唯一的“键”道具。 检查“搜索”的渲染方法 - Warning: Each child in an array or iterator should have a unique “key” prop. Check the render method of 'search' 警告:数组或迭代器中的每个子代均应具有唯一的“键”道具。 钥匙已经存在 - Warning: Each child in an array or iterator should have a unique “key” prop. Keys are already present React JS:警告:数组或迭代器中的每个子代都应具有唯一的“键”属性 - React JS : Warning: Each child in an array or iterator should have a unique “key” prop 反应错误index.js:1452警告:数组或迭代器中的每个子代都应具有唯一的“键”属性 - React error index.js:1452 Warning: Each child in an array or iterator should have a unique “key” prop 数组或迭代器中的每个孩子都应该有一个唯一的“key” prop - Each child in an array or iterator should have a unique "key" prop 数组或迭代器中的每个子代在reactjs中都应具有唯一的“键”道具 - Each child in an array or iterator should have a unique “key” prop in reactjs
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM