简体   繁体   English

React JS:警告:数组或迭代器中的每个子代都应具有唯一的“键”属性

[英]React JS : Warning: Each child in an array or iterator should have a unique “key” prop

I'm having some trouble while learning ReactJs. 我在学习ReactJs时遇到了一些麻烦。 I keep having this warning : 我不断收到此警告:

Warning: Each child in an array or iterator should have a unique "key" prop. 警告:数组或迭代器中的每个子代都应具有唯一的“键”道具。

What loses me is that I did gave a unique key to each of my elements, in that piece of code : 让我迷惑的是,我在那段代码中为每个元素都赋予了唯一的键:

let tab = props.todos.map((t) => {return <TodoListItem key={t.id} todo={t} onRemove={props.onRemove} /> })

My TodoListItems are <li> elements, I also tried to give them a key then I tested the existence and the value of {t.id} before giving it as a key and it's ok. 我的TodoListItems<li>元素,我还尝试为它们提供一个密钥,然后在将其作为密钥之前测试了{t.id}的存在和值,没关系。 I also cleared the cache before every execution. 在每次执行之前,我还清除了缓存。

I'm out of solutions, that's why I'm asking for your help. 我没有解决方案,这就是为什么我寻求您的帮助。

Thank you in advance. 先感谢您。

The Safest way to enforce a unique key is to use the index parameter of your map function. 强制执行唯一键的最安全方法是使用map函数的index参数。

let tab = props.todos.map((t, index) => {return <TodoListItem key={index} todo={t} onRemove={props.onRemove} /> })

If you truly want to use your t.id as the key, I would suggest temporarily changing your code to use the index (per above snippet), but dumping out your t.id in your rendered markup to validate that your id's are, in fact, unique. 如果您确实想使用t.id作为密钥,我建议暂时更改您的代码以使用索引(按上面的代码段),但是将您的t.id转储到呈现的标记中以验证您的ID在其实很独特。

暂无
暂无

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

相关问题 反应错误index.js:1452警告:数组或迭代器中的每个子代都应具有唯一的“键”属性 - React error index.js:1452 Warning: Each child in an array or iterator should have a unique “key” prop 警告:数组或迭代器中的每个子代都应具有唯一的“键”道具 - Warning :Each child in an array or iterator should have a unique “key” prop 警告:数组或迭代器中的每个孩子都应该有一个唯一的“键”道具 - Warning: Each Child in an Array or Iterator Should Have a Unique “Key” Prop React - 数组或迭代器中的每个子节点都应该有一个唯一的“key”prop - React - Each child in an array or iterator should have a unique “key” prop 警告:数组或迭代器中的每个子代都应具有唯一的“键”道具。 li中已添加的关键道具 - Warning: Each child in an array or iterator should have a unique “key” prop.; key prop already added in li 数组或迭代器中的每个子代都应具有唯一的“键”道具。 反应JS错误 - Each child in an array or iterator should have a unique “key” prop. React JS Error 持久警告:“即使已经设置了密钥,数组或迭代器中的每个子节点都应该具有唯一的&#39;密钥&#39;prop” - Persistent Warning: “Each child in an array or iterator should have a unique 'key' prop” even if key is already set 警告:数组或迭代器中的每个子代均应具有唯一的“键”道具。 检查`单位`的渲染方法 - Warning: Each child in an array or iterator should have a unique “key” prop. Check the render method of `Units` 警告:数组或迭代器中的每个子代均应具有唯一的“键”道具。 检查“搜索”的渲染方法 - Warning: Each child in an array or iterator should have a unique “key” prop. Check the render method of 'search' 警告:数组或迭代器中的每个子代均应具有唯一的“键”道具。 钥匙已经存在 - Warning: Each child in an array or iterator should have a unique “key” prop. Keys are already present
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM