简体   繁体   English

ESLint 错误:列表中的每个孩子都应该有一个唯一的“key”道具

[英]ESLint Error: Each child in a list should have a unique “key” prop

I have a JSON string like below.我有一个 JSON 字符串,如下所示。 The string has \n for the next line.该字符串具有 \n 用于下一行。

data:{a:"A computer is a machine that can be instructed to carry out sequences of arithmetic or logical operations automatically via computer programming. Modern computers have the ability to follow generalized sets of operations, called programs. These programs enable computers to perform an extremely wide range of tasks. A "complete" computer including the hardware, the operating system (main software), and peripheral equipment required and used for "full" operation can be referred to as a computer system. This term may as well be used for a group of computers that are connected and work together, in particular, a computer network or computer cluster.\nComputers are used as control systems for a wide variety of industrial and consumer devices. This includes simple special purpose devices like microwave ovens and remote controls, factory devices such as industrial robots and computer-aided design, and also general-purpose devices like personal computers and mobile devices such as smartphones. The Internet is run on computers and it connects hundreds of millions of other computers and their users."}

I used it in React like below:我在 React 中使用它,如下所示:

const b = data.a.split("\n");
return(
<P>
b.map((p,index)=>({p}<br key={index}/>))
</p>

I got an ES lint warning For the above code.对于上面的代码,我收到了 ES lint 警告。 The warning is like below.警告如下所示。

Each child in a list should have a unique "key" prop.

How could I avoid the warning?我怎样才能避免警告? I need a text in the paragraph tag and each line breaks in the text (\n) substitute by <br/>我需要段落标记中的文本,文本中的每个换行符(\n)替换为<br/>

b.map((p,index)=>(<p key={index}>{p}</p>))

You need the key on a wrapping element.您需要包装元素上的密钥。 Just use a p tag and you get the line break for free.只需使用p标签,您就可以免费获得换行符。 ;) ;)

There are reasons that using index as your key is a bad idea , though.不过,使用索引作为键是一个坏主意是有原因的。

You need to set key attribute to the item returned from map instead of <br> element.您需要将key属性设置为从map而不是<br>元素返回的项目。

Try something like this:尝试这样的事情:

<P>
  { React.Children.toArray(b.map((p,index)=>({p}<br/>))) }
</p>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 反应错误:列表中的每个孩子都应该有一个唯一的“关键”道具 - React Error: Each child in a list should have a unique "key" prop 列表中的每个孩子都应该有一个唯一的“关键”道具控制台错误 - Each child in a list should have a unique "key" prop console error 修复警告 ///列表中的每个孩子都应该有一个唯一的“键”道具 - fixing warning ///Each child in a list should have a unique "key" prop Reactjs:警告列表中的每个孩子都应该有一个唯一的“关键”道具 - Reactjs: Warning Each child in a list should have a unique "key" prop 警告:列表中的每个孩子都应该有一个唯一的“键”道具来切换 - Warning: Each child in a list should have a unique "key" prop for switch “反应”列表中的每个孩子都应该有一个唯一的“关键”道具 - "react" Each child in a list should have a unique "key" prop 列表中的每个孩子都应该有唯一的“关键”道具 - each child in a list should have unique 'key' prop React - 列表中的每个孩子都应该有一个唯一的“key”道具 - React - Each child in a list should have a unique “key” prop 列表中的每个孩子都应该有一个唯一的 key prop - Each child in a list should have a unique key prop ReactJs-列表中的每个孩子应该有一个唯一的“关键”道具 - ReactJs - Each child in a list should have a unique “key” prop
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM