简体   繁体   English

当每个键已经是唯一的时,列表中的每个孩子都应该有一个唯一的“键”警告

[英]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.

相关问题 为什么当已经有一个 key prop 时,这个警告会一直出现? 警告:列表中的每个孩子都应该有一个唯一的“key”道具 - Why does this warning keep appearing when there is already a key prop? Warning: Each child in a list should have a unique “key” prop 警告:列表中的每个孩子都应该有一个唯一的“key”道具。 即使已经设置了密钥 - Warning: Each child in a list should have a unique "key" prop. Even after setting the key already 像这样的警告:列表中的每个孩子都应该有一个唯一的“关键”道具 - warning like : Each child in a list should have a unique "key" prop 反应警告:列表中的每个孩子都应该有一个唯一的“关键”道具 - React Warning: Each child in a list should have a unique "key" prop 警告:列表中的每个孩子都应该有一个唯一的“key”道具 - Warning: Each child in a list should have a unique “key” prop 错误:警告:列表中的每个孩子都应该有一个唯一的“关键”道具 - ERR : Warning: Each child in a list should have a unique "key" prop 有一种方法可以解决“警告:列表中的每个孩子都应该有一个唯一的“密钥”? - there is a way to fix "Warning: Each child in a list should have a unique "key"? 警告列表中的每个孩子都应该有一个唯一的“关键”道具 - Warning Each child in a list should have a unique "key" prop 修复警告 ///列表中的每个孩子都应该有一个唯一的“键”道具 - fixing warning ///Each child in a list should have a unique "key" prop Reactjs:警告列表中的每个孩子都应该有一个唯一的“关键”道具 - Reactjs: Warning Each child in a list should have a unique "key" prop
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM