简体   繁体   English

警告:列表中的每个孩子都应该有一个唯一的“键”道具来切换

[英]Warning: Each child in a list should have a unique "key" prop for switch

Please tell me how to add a unique "key" prop for each child in a list.请告诉我如何为列表中的每个孩子添加一个唯一的“关键”道具。 It is a functional component in react:它是反应中的一个功能组件:

<List component="nav">
  {bundlesData.map((bundleItem) => {
    switch (bundleItem) {
      case 'page':
        return pagesNavLink

      case 'menu':
        return (
          <MenuNavLink
            handleClick={handleClick}
            isOpened={isNavLinkOpened}
          />
        )

      default:
        return bundleItem
    }
  })}
</List>

You can use the index to identify each bundleItem :您可以使用index来识别每个bundleItem

<List component='nav'>
  {bundlesData.map((bundleItem, index) => {
    switch (bundleItem) {
      case 'page':
        return <div key={index}>{pagesNavLink}</div>;

      case 'menu':
        return (
          <MenuNavLink
            key={index}
            handleClick={handleClick}
            isOpened={isNavLinkOpened}
          />
        );

      default:
        return <div key={index}>{bundleItem}</div>;
    }
  })}
</List>;

One thing you can do with a map function is it can take a second parameter which keeps track of the index.您可以使用 map function 做的一件事是它可以采用第二个参数来跟踪索引。 You can then use that index parameter on the list items as a key prop.然后,您可以在列表项上使用该索引参数作为关键道具。

Source: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map来源: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map

暂无
暂无

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

相关问题 像这样的警告:列表中的每个孩子都应该有一个唯一的“关键”道具 - warning like : Each child in a list should have a unique "key" prop 反应警告:列表中的每个孩子都应该有一个唯一的“关键”道具 - React Warning: Each child in a list should have a unique "key" prop 警告:列表中的每个孩子都应该有一个唯一的“key”道具 - Warning: Each child in a list should have a unique “key” prop 错误:警告:列表中的每个孩子都应该有一个唯一的“关键”道具 - ERR : Warning: Each child in a list should have a unique "key" prop 修复警告 ///列表中的每个孩子都应该有一个唯一的“键”道具 - 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 错误警告:列表中的每个孩子都应该有一个唯一的“关键”道具 - ERROR Warning: Each child in a list should have a unique "key" prop 警告列表中的每个孩子都应该有一个唯一的“关键”道具 - Warning Each child in a list should have a unique "key" prop 添加了关键道具 || 警告:列表中的每个孩子都应该有一个唯一的“key”道具 - Added key prop || Warning: 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
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM