[英]React - Each child in a list should have a unique “key” prop
I am working on a React Web Application Where I fetch and display student data from an API and I keep getting the error,我正在开发一个 React Web 应用程序,我从 API 获取并显示学生数据,但我一直收到错误,
Warning: Each child in a list should have a unique "key" prop.警告:列表中的每个孩子都应该有一个唯一的“关键”道具。 Check the render method of
Accordion
检查
Accordion
的渲染方法
Accordion.js being one of my components of my Web Application Accordion.js 是我的 Web 应用程序的组件之一
Any Help to fix this issue would be appreciated:)任何解决此问题的帮助将不胜感激:)
I have tried passing a key prop with an id to the Accordion component from a parent component, but that did not seem to work.我尝试将带有 id 的 key prop 从父组件传递给 Accordion 组件,但这似乎不起作用。 Could it be that I need to pass a key in my test score paragraph?
难道是我需要在我的考试成绩段落中传递一个关键吗?
On line 23在线 23
Make the following change进行以下更改
<p
To至
<p key={`${i}-stats`}
The issue is simple.问题很简单。 React needs to identify each sibling node uniquely and thus it asks you to pass key prop to sibling nodes when returning array or iterating over a lost to return component/view.
React 需要唯一地标识每个兄弟节点,因此它要求您在返回数组或迭代丢失以返回组件/视图时将 key prop 传递给兄弟节点。
It is not recommended to use counter (0, 1, 2...) as your key when populating children.在填充子项时,不建议使用 counter (0, 1, 2...)作为您的键。
You should use uuid() or item's self-identifier .您应该使用uuid()或 item 的self-identifier 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.