简体   繁体   English

列表中的每个孩子都应该有一个唯一的“关键”道具错误,以 uuid 作为关键反应

[英]Each child in a list should have a unique "key" prop error with uuid as key react

I'm getting an "Each child in a list should have a unique "key" prop."我得到一个“列表中的每个孩子都应该有一个唯一的“关键”道具。” in console here specifically (it quotes the first line as the relevant line)在控制台中专门(它引用第一行作为相关行)

<Dropdown.Menu variant="dark">
  {[
    [0, "prod_name", "Name"],
    [1, "price", "Price"],
    [2, "average_rating", "Rating"],
  ].map((item, i) => (
    <>
      <Dropdown.Item
        as={Button}
        key={uuid.v4()}
        onClick={() => {
          this.setState({ sort: item[0], open: false });
          this.context.sort(item[1], "asc");
        }}
        className={
          this.state.sort === item[0]
            ? "text-black giBold active"
            : "text-light"
        }
      >
        {item[2] + " (ascending)"}
      </Dropdown.Item>
      <Dropdown.Item
        as={Button}
        key={uuid.v4()}
        onClick={() => {
          this.setState({ sort: item[0] + 3, open: false });
          this.context.sort(item[1], "desc");
        }}
        className={
          this.state.sort === item[0] + 3
            ? "text-black giBold active"
            : "text-light"
        }
      >
        {item[2] + " (descending)"}
      </Dropdown.Item>
    </>
  ))}
</Dropdown.Menu>;
               

I changed the key to be uuid since I realised tonnes of id's of different items are going to be the same in hopes that it would fix the error yet it keeps popping up.我将密钥更改为 uuid,因为我意识到大量不同项目的 id 将是相同的,希望它能修复错误但它不断弹出。

Is there something else at play here that I've missed, I tried looking for answers but couldn't find much.这里还有其他我错过的东西吗,我试图寻找答案,但找不到太多。

Oh, you just need to add the key to <> , because the key property should be in the first tag in the map function's render section and change the empty tag to <div> because the empty tag can not have any property.哦,你只需要在<>中添加 key ,因为 key 属性应该在map函数的渲染部分的第一个标签中,并将空标签更改为<div> ,因为空标签不能有任何属性。 Your code should look like this;您的代码应如下所示;


[...].map((item, i) => (
    <div key={uuid.v4()}> // this tag's key important for map function
      <Dropdown.Item
        as={Button}
        key={uuid.v4()}
        ...
      >
        {item[2] + " (ascending)"}
      </Dropdown.Item>
      <Dropdown.Item
        as={Button}
        key={uuid.v4()}
        ...
      >
        {item[2] + " (descending)"}
      </Dropdown.Item>
    </div>
  ))}
</Dropdown.Menu>;

暂无
暂无

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

相关问题 反应错误:列表中的每个孩子都应该有一个唯一的“关键”道具 - React Error: Each child in a list should have a unique "key" prop “反应”列表中的每个孩子都应该有一个唯一的“关键”道具 - "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 列表中的每个孩子都应该有一个唯一的“关键”道具/反应 - Each child in a list should have a unique "key" prop / react 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 ESLint 错误:列表中的每个孩子都应该有一个唯一的“key”道具 - ESLint 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 使用 Formik 错误响应表单:列表中的每个子项都应具有唯一的“键”属性 - React form with Formik error : Each child in a list should have a unique "key" prop
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM