简体   繁体   English

列表中的每个孩子都应该有一个唯一的 key prop

[英]Each child in a list should have a unique key prop

Here is a link to a working example of the site: https://codesandbox.io/s/eloquent-kapitsa-kk1ls这是该站点的工作示例的链接: https://codesandbox.io/s/eloquent-kapitsa-kk1ls

The issue I am having is with Buttons.js:我遇到的问题是 Buttons.js:

import React, { Component } from "react";
import { FontAwesomeIcon } from "@fortawesome/react-fontawesome";
import registerIcons from "./FontAwesome";

registerIcons();

const DATA = [
  {
    href: "https://github.com/",
    icon: ["fab", "github"],
    label: "Github"
  },
  {
    href: "https://www.linkedin.com/in//",
    icon: ["fab", "linkedin"],
    label: "LinkedIn"
  },
  {
    href: "...",
    icon: ["fas", "file-alt"],
    label: "Resume"
  },
  {
    href: "mailto:",
    icon: ["fas", "paper-plane"],
    label: "Email me"
  }
];

const Icon = ({ href, icon, label }) => {
  return (
    <span className="button">
      <a href={href} target="_self" rel="noopener noreferrer">
        <FontAwesomeIcon className="icon" icon={icon} size="3x" />
        <span className="icon_title">{label}</span>
      </a>
    </span>
  );
};

class Buttons extends Component {
  render() {
    return (
      <div>
        {DATA.map(props => (
          <Icon {...props} />
        ))}
      </div>
    );
  }
}

export default Buttons;

I have looked through the other topics related to this issue and none are analogous to my case.我查看了与此问题相关的其他主题,但没有一个与我的情况类似。 I am not passing parameters to the render method - just taking an existing variable and mapping it to produce my output.我没有将参数传递给渲染方法 - 只是获取现有变量并将其映射以生成我的 output。

I have tried changing the Buttons render method to the code below in addition to a few other permutations of the DATA array without much progress.除了 DATA 数组的其他一些排列之外,我还尝试将Buttons渲染方法更改为下面的代码,但没有太大进展。

      <div key={DATA.label}>
        {DATA.map(props => (
          <Icon {...props} />
        ))}
      </div>

I have also read through the React documentation on keys with no success.我还阅读了关于键的 React 文档,但没有成功。

you should change the code with this:你应该用这个改变代码:

<div>
   {DATA.map((props,i) => (
      <Icon key={i} {...props} />
   ))}
</div>

As it is advised not to use index as a key for component.因为建议不要使用索引作为组件的键。 I would say add a random number and do something like below我会说添加一个随机数并执行以下操作

<div>
   {DATA.map((props,i) => (
      <Icon key={Math.random()*1000*i} {...props} />
   ))}
</div>

This is rather a generic approach if you don't have any unique key in your object.如果您的 object 中没有任何唯一键,这是一种相当通用的方法。

The key needs to go on like this:密钥需要 go 就这样:

<div key={DATA.label}>
 {DATA.map(props => (
   <Icon key={props.label} {...props} />
  ))}
</div>

As you probably saw in the React documentation on keys you reference, they say正如你可能在 React 文档中看到的关于你引用的键的那样,他们说

The best way to pick a key is to use a string that uniquely identifies a list item among its siblings.选择键的最佳方法是使用一个字符串,该字符串在其兄弟项中唯一标识一个列表项。

Each of the objects in your DATA array have unique strings and you can use any of them key={props.href} or key={props.icon[1]} . DATA 数组中的每个对象都有唯一的字符串,您可以使用其中任何一个key={props.href}key={props.icon[1]} I used props.label in the answer above but the point is it doesn't have to be totally unique in all the world, it just needs to be unique compared to the other items in the list.我在上面的答案中使用了 props.label 但关键是它不必在全世界都是独一无二的,它只需要与列表中的其他项目相比是独一无二的。

Try尝试

<Icon {...props} key={props.href} />

From react documentation来自反应文档

A good rule of thumb is that elements inside the map() call need keys.一个好的经验法则是 map() 调用中的元素需要键。

Please take a look to this documentation请查看此文档

https://reactjs.org/docs/lists-and-keys.html#extracting-components-with-keys https://reactjs.org/docs/lists-and-keys.html#extracting-components-with-keys

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

相关问题 修复警告 ///列表中的每个孩子都应该有一个唯一的“键”道具 - 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 列表中的每个孩子都应该有一个唯一的“关键”道具,检查方法 - Each child in a list should have a unique "key" prop, check method 错误警告:列表中的每个孩子都应该有一个唯一的“关键”道具 - ERROR Warning: Each child in a list should have a unique "key" prop ReactJs-列表中的每个孩子应该有一个唯一的“关键”道具 - ReactJs - Each child in a list should have a unique “key” prop 列表中的每个孩子都应该有一个唯一的“关键”道具/反应 - Each child in a list should have a unique "key" prop / react
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM