[英]Error: Missing "key" prop for element in iterator react/jsx-key
Error: Missing "key" prop for element in iterator react/jsx-key info - Need to disable some ESLint rules?错误:在迭代器 react/jsx-key 信息中缺少元素的“key”道具 - 需要禁用一些 ESLint 规则?
return (
<div>
<div>
<BreadcrumbBlogs />
</div>
<div className={styles.container}>
<div className={styles.blogblock}>
{newsposts.slice(0, 6).map((post) => (
<div className={styles.blogcontent} key={post.id}>
<Image
src={post.jetpack_featured_media_url}
width={600}
height={400}
alt="thumbnail"
></Image>
<Link href={`${post.id}`} className={styles.blogcolumn}>
<div
className={styles.blogtitile}
dangerouslySetInnerHTML={{ __html: post.title.rendered }}
></div>
</Link>
</div>
))}
</div>
</div>
</div>
); );
To fix this issue, add key={post.id}
to your div
element instead of the Link
element.要解决此问题,请将
key={post.id}
添加到您的div
元素而不是Link
元素。 The ESLint rule in this case is providing valuable feedback so I wouldn't disable it.在这种情况下,ESLint 规则提供了有价值的反馈,所以我不会禁用它。
In React, when you want to create multiple components, you have to provide a "key" for every child component, and that key has to be a unique value.在 React 中,当您想要创建多个组件时,您必须为每个子组件提供一个“键”,并且该键必须是唯一的值。
So, in order to solve that problem, add the key prop to the div所以,为了解决这个问题,将 key prop 添加到 div
<div className={styles.blogcontent} key={post.id}>
In case you don't have an id, you can use anything that is unique (like the array index for example).如果您没有 id,您可以使用任何唯一的东西(例如数组索引)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.