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