[英]Each child in a list should have a unique “key” warning when every key is already unique
Can somebody tell me why I get Each child in a list should have a unique "key" prop
even after I created a unique key for every element?有人能告诉我为什么我得到
Each child in a list should have a unique "key" prop
,即使我为每个元素创建了一个唯一的键?
I can access every key by keys[index].tag-name
from the keys
object array and verify that it is unique.我可以通过
keys
object 数组中的keys[index].tag-name
访问每个键,并验证它是唯一的。
return (
Object.entries(props.values).map(([key, value], index) => (
<div>
<span key={keys[index].span} />
<p key={keys[index].p} />
</div>
))
);
// key format "c98301e0-f2bd-4442-a829-407e6beecf0d"
You must add key property to a parent element.您必须将 key 属性添加到父元素。 In your case that's a
div
.在你的情况下,这是一个
div
。
return (
Object.entries(props.values).map(([key, value], index) => (
<div key={keys[index].span} >
<span/>
<p key={keys[index].p} />
</div>
))
);
Try to put the key into div, maybe this can solve your problem尝试将密钥放入div,也许这可以解决您的问题
return (
Object.entries(props.values).map(([key, value], index) => (
<div key={keys[index].span}>
<span />
<p />
</div>
))
);
You have to add the key to the parent that is being returned, here div
您必须将密钥添加到正在返回的父级,这里是
div
return (
Object.entries(props.values).map(([key, value], index) => (
<div key={keys[index].div}>
<span />
<p />
</div>
))
);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.