[英]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.