[英]Ionic + React.js: Warning: Each child in a list should have a unique “key” prop
I am utilizing firebase for the database and Ionic + React for mobile app.我将 firebase 用于数据库,将 Ionic + React 用于移动应用程序。 I already converted my firebase data into array but when I want to map the values.
我已经将我的 firebase 数据转换为数组,但是当我想要 map 值时。 It tells me that it should have a unique key, but I already put a unique key in my return function in the element.
它告诉我它应该有一个唯一键,但我已经在元素中的返回 function 中放置了一个唯一键。 Can someone tell me what did I do wrong?
有人能告诉我我做错了什么吗? Thanks.
谢谢。
Here is my code in converting objects to array这是我将对象转换为数组的代码
const Tab2: React.FC = () => {
const [doctors, setDoctor] = React.useState([]);
useIonViewWillEnter(()=>{
console.log('Enter')
firebase.database().ref('users').orderByChild('type').equalTo('doctor').on('value',(snapshot)=>{
setDoctor(Object.keys(snapshot.val()).map(key => ({ [key]: snapshot.val()[key] })))
})
console.log(doctors)
})
And in my return而在我的回报
<IonList>
{doctors.map(elem => {
return(
<IonItem key={elem['uid']}>
<IonLabel>
<IonText className="font-weight: bold;">
<h3>{elem['name']}</h3>
</IonText>
<h4>{elem['speciality']}</h4>
<h4>{elem['email']}</h4>
</IonLabel>
<br></br>
</IonItem>
)
})}
</IonList>
What I got is Warning: Each child in a list should have a unique "key" prop.
我得到的是
Warning: Each child in a list should have a unique "key" prop.
My firebase structure我的firebase结构
Update: the console.log(doctors)
will only output an empty array like this []
and i dont know why.更新:
console.log(doctors)
只会 output 像这样的空数组[]
我不知道为什么。 I already put it in a method before the component enter.在组件进入之前,我已经把它放在了一个方法中。
Note: thats just a warning, not an error.注意:这只是一个警告,而不是错误。
This is the right approach.这是正确的做法。
<IonList>
{doctors.map((elem, index) => {
// index has to come from the second parameter from map
return (
<IonItem key={index}>
<IonLabel>
<IonText className="font-weight: bold;">
<h3>{elem["name"]}</h3>
</IonText>
<h4>{elem["speciality"]}</h4>
<h4>{elem["email"]}</h4>
</IonLabel>
<br></br>
</IonItem>
);
})}
</IonList>;
The index must be placed in the first/parent div element.索引必须放在第一个/父 div 元素中。 In this case it will be
<IonItem>
在这种情况下,它将是
<IonItem>
ref: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map参考: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map
Also as per you're not getting the data.另外,根据您没有获取数据。 Adjust your code like so.
像这样调整你的代码。 You just have to create a new arr, and move through snapshot to get a key.
您只需要创建一个新的 arr,然后通过快照来获取密钥。
pushing the object with the respective key to a new arr, so it can be iterable without specific key names such as eA9w5ynhqN9iT7kKDBt2Ane9VF3将带有相应密钥的 object 推送到新的 arr,因此它可以在没有特定密钥名称的情况下进行迭代,例如eA9w5ynhqN9iT7kKDBt2Ane9VF3
useIonViewWillEnter(() => {
console.log("Enter");
let newArr = [];
firebase
.database()
.ref("users")
.orderByChild("type")
.equalTo("doctor")
.on("value", (snapshot) => {
let key;
snapshot.forEach((childSnap) => {
key = childSnap.key; // im just getting the key
const snapVal = snapshot.val(); // getting the object
newArr.push(snapVal[key]);
});
setDoctor(newArr);
// console.log(doctors)
});
});
try adding the key
prop to each child as well as each element inside children and it will work.尝试将
key
prop 添加到每个孩子以及孩子中的每个元素,它会起作用。
your code should look like this:您的代码应如下所示:
<IonList>
{doctors.map(elem => {
return(
<IonItem key={elem['uid']}>
<IonLabel key={"lbl-" + elem['uid']}>
<IonText key={"txt-"+elem['uid']}className="font-weight: bold;">
<h3 key={"name-"+elem['uid']}>{elem['name']}</h3>
</IonText>
<h4 key={"speciality-"+elem['uid']}>{elem['speciality']}</h4>
<h4 key={"email-"+elem['uid']}>{elem['email']}</h4>
</IonLabel>
<br></br>
</IonItem>
)
})}
</IonList>
if you noticed i added lbl-, txt- before the key and that to avoid another warning: "Warning: flattenChildren(...): Encountered two children with the same key,如果您注意到我在密钥之前添加了 lbl-, txt- 以避免另一个警告:“警告:flattenChildren(...): Encountered two children with the same key,
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.