简体   繁体   English

列表中的每个孩子都应该有一个唯一的“关键”道具/反应

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

I am struggling get rid of this error message.我正在努力摆脱此错误消息。 What did I do wrong in my code?我的代码做错了什么? I can't see where else I should be adding a key.我看不出应该在哪里添加密钥。

import { AiOutlineLaptop } from "react-icons/ai";
import { FaBootstrap, FaReact } from "react-icons/fa";
const data = [
  { "Id": "1",
    "title": "Create Components",
    "text": "Lorem ipsum dolor sit amet consectetur.",
    "icon": AiOutlineLaptop,
    "icon1": "fas fa-shopping-cart",
  },
  {
    "Id": "2",
    "title": "Data Input",
    "text": "Lorem ipsum dolor sit amet consectetur.",
    "icon": "fas fa-circle fa-stack-2x",
    "icon1": "fas fa-laptop fa-stack-1x fa-inverse",
  },
  { "Id": "3",
    "title": " React Life Cycle Method",
    "text": "Lorem ipsum dolor sit amet consectetur.",
    "icon": "fas fa-circle fa-stack-2x",
    "icon1": "fab fa-btc fa-stack-1x fa-inverse",
  },

];

const icons = [
  AiOutlineLaptop,
  FaReact,
  FaBootstrap,
];


const Skills = () => {
  return (
    <>
      <section className="page-section" id="services">
        <div className="container">
          <div className="text-center">
            <h2 className="section-heading text-uppercase">Skills</h2>
            <h3 className="section-subheading text-muted">Lorem ipsum dolor sit amet consectetur.</h3>
          </div>
          <div className="row text-center">

            {data.map((link, idx) => {
              const Icon = icons[idx];
              return (
                <div className="col-md-4">
                  <Icon className="icon" key={icons[idx]} />
                  <h4 className="my-3" key={idx}>{link.title}</h4>
                  <p className="text-muted">{link.text}</p>
                </div>
              )
            })}

          </div>
        </div>
      </section>
    </>
  );
}

export default Skills;  

Every item in the iteration needs a unique identifier for React's internal working.迭代中的每一项都需要一个唯一的标识符,用于 React 的内部工作。 Add key to the outermost elementkey添加到最外层元素

<div className="col-md-4" key={link.id}>

When using map to render components in React you should always provide a unique key to the top element, in your case, the simple way of doing it would be using the index (idx) like so在 React 中使用 map 渲染组件时,您应该始终为顶部元素提供一个唯一键,在您的情况下,这样做的简单方法是使用索引 (idx),如下所示

 {data.map((link, idx) => {
          const Icon = icons[idx];
          return (
            <div key={idx} className="col-md-4">
              <Icon className="icon" key={icons[idx]} />
              <h4 className="my-3" key={idx}>{link.title}</h4>
              <p className="text-muted">{link.text}</p>
            </div>
          )
        })}

暂无
暂无

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

相关问题 “反应”列表中的每个孩子都应该有一个唯一的“关键”道具 - "react" Each child in a list should have a unique "key" prop React - 列表中的每个孩子都应该有一个唯一的“key”道具 - React - Each child in a list should have a unique “key” prop 反应错误:列表中的每个孩子都应该有一个唯一的“关键”道具 - React Error: Each child in a list should have a unique "key" prop React - 列表中的每个孩子都应该有一个唯一的“key”道具 - React - Each child in a list should have a unique “key” prop 反应警告:列表中的每个孩子都应该有一个唯一的“关键”道具 - React Warning: Each child in a list should have a unique "key" prop React,列表中的每个孩子都应该有一个唯一的 key prop - React, Each child in a list should have a unique key prop 警告:列表中的每个孩子都应该有一个唯一的“key”道具,即使它有唯一的 key React - Warning: Each child in a list should have a unique “key” prop even tho it has unique key React React - 数组或迭代器中的每个子节点都应该有一个唯一的“key”prop - React - Each child in an array or iterator should have a unique “key” prop 反应警告:列表中的每个孩子都应该有一个唯一的“关键”道具不是由地图中缺少关键引起的 - React Warning: Each child in a list should have a unique "key" prop NOT caused by lack of key in map 列表中的每个孩子都应该有一个唯一的“关键”道具错误,以 uuid 作为关键反应 - Each child in a list should have a unique "key" prop error with uuid as key react
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM