【发布时间】:2022-01-10 07:21:30
【问题描述】:
我正在使用 import "react-native-form-select-picker" 在我的 react native 应用程序中进行选择输入,并且代码工作正常,但它仍然给我一个警告,因为 "Encountered Two Children with the same key , `.$1/.$2".那么我该如何解决这个问题有人可以帮忙吗?以下是我的代码行:
import React, { useState } from 'react';
import { View, Text, StyleSheet, TouchableOpacity, } from 'react-native';
import SelectPicker from 'react-native-form-select-picker';
const options = ["Aadhar Card", "Bank Passbook", "Driving Licence", "Pan Card"];
// create a component
const UpdateDocument = ({ navigation }) => {
const [selected, setSelected] = useState();
return (
<View style={styles.container}>
<View style={styles.docBox}>
<SelectPicker
placeholder="Select document to Update"
style={styles.selectBox}
onValueChange={(value) => {
// Do anything you want with the value.
// For example, save in state.
setSelected(value);
}}
selected={selected}
>
{Object.values(options).map((val, index) => (
<SelectPicker.Item label={val} value={val} key={} />
))}
</SelectPicker>
</View>
</View>
);
};
// define your styles
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: '#fff',
},
docBox: {
paddingVertical: 10,
paddingHorizontal: 10,
borderBottomColor: '#aaa',
borderBottomWidth: 1,
},
selectBox: { borderColor: '#555', borderWidth: 1, borderRadius: 4, }
});
//make this component available to the app
export default UpdateDocument;
【问题讨论】:
-
正如错误所说。
key={}key不会因不同的孩子而改变 - 你不能这样做,你需要为每个孩子选择一个唯一的键。
标签: javascript arrays react-native unique-key