简体   繁体   English

错误:在迭代器 react/jsx-key 中缺少元素的“key”道具

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

); );

Screenshot截屏

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.

相关问题 React 中的迭代器 react/jsx-key 中缺少元素的“key”属性 - Missing “key” prop for element in iterator react/jsx-key in React eslint:缺少迭代器中元素的“key”道具(react/jsx-key) - eslint: Missing "key" prop for element in iterator (react/jsx-key) React MUI 错误缺少数组 react/jsx-key 中元素的“键”道具 - React MUI Error Missing "key" prop for element in array react/jsx-key iteratoreslintreact/jsx-key 中缺少元素的“键”道具 - Missing "key" prop for element in iteratoreslintreact/jsx-key 如何解决错误问题:当我 map react-icons 时,数组 react/jsx-key 中的元素缺少“key”道具? - how to solve problem with Error: Missing "key" prop for element in array react/jsx-key when I map react-icons? 反应项目脚本 linter 错误迭代器中缺少元素的“关键”道具 - React project script linter error Missing "key" prop for element in iterator 迭代器中缺少元素的“键”道具 - Missing "key" prop for element in iterator 迭代器中缺少元素的“关键”道具并且禁止传播道具 - Missing "key" prop for element in iterator and Prop spreading is forbidden React esLint 错误:缺少数组中元素的“key”道具 - React esLint error: Missing "key" prop for element in array 在下一个迭代器中缺少元素的“键”道具,js typescript - Missing "key" prop for element in iterator in next,js typescript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM