简体   繁体   English

React 应用程序抛出关于键的错误,但我已经在顶层分配了一个唯一键

[英]React app throwing error about key, but I have already assigned a unique key at the top level

Here is the code throwing the error with the console message below:以下是通过以下控制台消息引发错误的代码:

                    <div className="col-12">
                        <h6>Infractions:</h6>
                        {infractions.map(({ id, itype, dateStamp }) => (
                            <li key={id} className="col-12">
                                {itype} {dateStamp}
                            </li>
                        ))}
                    </div>

and the error和错误

在此处输入图像描述

The error you are seeing is because the keys you have set are not unique, or undefined.您看到的错误是因为您设置的键不是唯一的或未定义的。

From what you have answered in the comments, you are using some kind of UUID to generate said id values.根据您在评论中的回答,您正在使用某种 UUID 来生成所述id值。 Since you haven't included that code in your question, you should probably verify the following:由于您没有在问题中包含该代码,因此您可能应该验证以下内容:

  • The UUIDs you are generating are indeed unique, and you are not accidentally reusing them or setting the same UUID for all records.您生成的 UUID 确实是唯一的,您不会意外地重用它们或为所有记录设置相同的 UUID。

  • The id field is always populated and React doesn't render the list with the values still undefined id字段总是被填充,并且 React 不会呈现值仍然undefined的列表

To check both those suggestions, and any further investigations you may need, you have a variety of tools available.要检查这些建议以及您可能需要的任何进一步调查,您可以使用多种工具。

  • You can use console.log and output the values to the terminal (it's not the cleanest approach, but it gives quick results)您可以使用console.log和 output 将值发送到终端(这不是最干净的方法,但可以快速得出结果)

  • You can also show the ID in the component, and see what's going on:您还可以在组件中显示 ID,并查看发生了什么:

<li key={id} className="col-12">
  (ID: {id}) - {itype} {dateStamp}
</li>

暂无
暂无

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

相关问题 关于 Formik 的反应抛出错误应该在每个中都有一个唯一的键 - React throw error about Formik should have a unique key in each 列表中的每个孩子都应该有一个唯一的“关键”道具,我在我的反应原生应用程序中遇到了这个错误 - Each child in a list should have a unique "key" prop, I am getting this error in my react native app 当我分配了一个键时,为什么会出现“列表中的每个子项都应该有一个唯一的“键”道具错误”? - Why am I getting 'Each child in a list should have a unique "key" prop error' When I have a key assigned? 反应递归手风琴组件抛出错误列表中的每个孩子都应该有一个唯一的关键道具 - react recursive accordion component throwing an error each child in a list should have a unique key prop 反应错误:列表中的每个孩子都应该有一个唯一的“关键”道具 - React Error: Each child in a list should have a unique "key" prop 尽管所有元素都有唯一标识符,但反应键错误 - React key error although all elements have unique identifier 我已经输入了一个独特的“关键”道具,但仍然有错误要求我在 React 中输入它 - I have input a unique "key" prop but still got error asking me to input it in React 我的组件中已经有一个密钥,但为什么它仍在寻找唯一的密钥? - i already have a Key in my component but why is it still looking for a unique key? 当唯一标识符已经存在时,对缺少唯一“关键”道具的错误做出反应 - React error with missing unique "key" prop when unique identifiers are already present 如何搜索嵌套字典而不关心顶级键 - How to search a nested dictionary and not care about top level key
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM