简体   繁体   English

React esLint 错误:缺少数组中元素的“key”道具

[英]React esLint error: Missing "key" prop for element in array

This is my Code这是我的代码

 <RadioButtons type="test" field="test" optionInclude={['1', '2', '3']} optionOrder={['1', '2', '3']} updateValue={[ { key: '1', value: 'test' }, { key: '2', value: 'test2' }, { key: '3', value: 'Other' } ]} icons={[ <Icon variant="male" size={35} />, <Icon variant="female" size={35} />, <Icon variant="plus" size={35} /> ]} />

When runnning EsLint i get this error:运行 EsLint 时出现此错误:

70:33 error Missing "key" prop for element in array react/jsx-key 71:33 error Missing "key" prop for element in array react/jsx-key 72:33 error Missing "key" prop for element in array

Line 70 - 72 is the Icons Array so that is what the error is referring to.第 70 - 72 行是Icons Array,因此这就是错误所指的内容。

The PropTypes for the RadioButtons component are RadioButtons组件的 PropTypes 是

 RadioButtons.propTypes = { ... icons: PropTypes.array, };

I thought this error was for when you iterate without the key prop.我认为这个错误是因为你在没有 key 道具的情况下进行迭代。

Since this question has no answer, I'll just reiterate what's in the comments: when you repeate any element in React (including your custom Icon element) those repeated elements must all have key attributes defined.由于这个问题没有答案,我只重申评论中的内容:当您在 React 中重复任何元素(包括您的自定义Icon元素)时,这些重复元素必须都定义了key属性。

Long Explanation About the Virtual DOM For the Curious关于虚拟 DOM 的详细解释

The reason for this has to do with React's Virtual DOM.这样做的原因与 React 的 Virtual DOM 有关。 A simple understanding of React works like this:对 React 的简单理解是这样的:

  1. React renders your components to the page's DOM React 将你的组件渲染到页面的 DOM
  2. Events happen, changing state事件发生,改变状态
  3. React re-renders the components that used that state to the DOM React 将使用该状态的组件重新渲染到 DOM

But if it was that simple, React would be incredibly wasteful.但如果就这么简单,React 会非常浪费。 Imagine for instance you have a <ul> ...想象一下,例如你有一个<ul> ...

<ul>
  <li>Item #1</li>
  <li>Item #2</li>
  <li>Item #3</li>
  <!--... -->
  <li>Item #25</li>
</ul>

(Presumably you're using a map to generate that in React, but how exactly you make the <ul> is irrelevant.) (大概您正在使用map在 React 中生成它,但是您如何制作<ul>无关紧要。)

Now, imagine that you move item #25 to be the first item in the list: if you simply re-rendered everything, you'd have to re-render 25 <li> elements ... even though only one of them changed.现在,假设您将第 25 项移动到列表中的第一项:如果您简单地重新渲染所有内容,则必须重新渲染 25 个<li>元素……即使其中只有一个发生了变化。

To avoid such unnecessary (and very costly, performance-wise) DOM changes, React uses a copy of the DOM, known as the Virtual DOM, and the actual process looks more like:为了避免这种不必要的(并且非常昂贵的,性能方面的)DOM 更改,React 使用了 DOM 的副本,称为虚拟 DOM,实际过程看起来更像:

  1. React renders your components to the page's DOM React 将你的组件渲染到页面的 DOM
  2. Events happen, changing state事件发生,改变状态
  3. React re-renders the components that used that state to the Virtual DOM React 将使用该状态的组件重新渲染到虚拟DOM
  4. React does a "diff" of the old and new Virtual DOMs, and only updates the actual DOM for the parts that changed in the virtual one React 对新旧虚拟 DOM 进行“差异化”,并且更新虚拟 DOM 中发生变化的部分的实际DOM

This is a major performance enhancement of using React (vs. using say JQuery's .html() method to accomplish the same thing)!这是使用 React 的主要性能增强(与使用 JQuery 的.html()方法来完成相同的事情相比)!

But do you remember when we moved item #25 to #1 a moment ago?但是您还记得我们刚才将第 25 项移到第 1 项的时候吗? From React's perspective, that could have been a move of #25 to #1 ... or it could have been the deletion of #25 and the insertion of a brand new <li> at #1 ... or it could have been a series of updates (of #1 => 25, #2 => #1, #3 => #2, ...)从 React 的角度来看,这可能是 #25 到 #1 的移动......或者它可能是删除 #25 并在 #1 处插入一个全新的<li> ......或者它可能是一系列更新(#1 => 25,#2 => #1,#3 => #2,...)

In other words, React has no way of magically knowing which <li> is which.换句话说,React 无法神奇地知道哪个<li>是哪个。 Without a unique identifier on each one, a moved element can look like stuff got deleted/created, or like lots of stuff got updated.如果每个元素都没有唯一标识符,移动的元素可能看起来像是被删除/创建的东西,或者很多东西被更新了。

This is where the key prop comes in: whenever you repeat elements (like those <li> s), React has difficulty telling them apart ... but it needs to to be able to use the Virtual DOM to make your app faster.这就是key道具的用武之地:每当您重复元素(如那些<li> s)时,React 就很难将它们区分开来……但它需要能够使用虚拟 DOM 来使您的应用程序更快。

This is why it's not required that you add a key to every repeated element (React will still work if you don't) ... but you will get lots of warnings about it ... because it means you're making your app needlessly slower.这就是为什么不需要你为每个重复的元素添加一个键(如果你不这样做,React 仍然可以工作)......但是你会收到很多关于它的警告......因为这意味着你正在制作你的应用程序不必要的慢。

声明:本站的技术帖子网页,遵循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 使用 create-react-app 创建应用程序后,数组中的元素缺少“key”道具 - Missing "key" prop for element in array after creating app with create-react-app Eslint错误处理反应/属性类型 - Eslint error handling react/prop-types ESLint 错误:列表中的每个孩子都应该有一个唯一的“key”道具 - ESLint Error: Each child in a list should have a unique “key” prop 道具验证中缺少 React eslint 错误 - React eslint error missing in props validation 反应导航错误:此导航器缺少导航道具 - React Navigation Error: The navigation prop is missing for this navigator 反应管理员 | 未捕获的错误:缺少历史道具 - React Admin | Uncaught Error: Missing history prop 当唯一标识符已经存在时,对缺少唯一“关键”道具的错误做出反应 - React error with missing unique "key" prop when unique identifiers are already present 数组或迭代器中的每个子代都应具有唯一的“键”道具。 反应JS错误 - Each child in an array or iterator should have a unique “key” prop. React JS Error 反应错误index.js:1452警告:数组或迭代器中的每个子代都应具有唯一的“键”属性 - React error index.js:1452 Warning: Each child in an array or iterator should have a unique “key” prop
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM