简体   繁体   English

警告:列表中的每个孩子都应该有一个唯一的“关键”道具。 如何解决这个问题?

[英]Warning: Each child in a list should have a unique "key" prop. how to fix this?

Ive been using this project with out a problem and now all of a sudden I keep getting this error and it won't show my notes when I click on the my notes section.我一直在使用这个项目没有问题,现在突然间我一直收到这个错误,当我点击我的笔记部分时它不会显示我的笔记。 What do I have to do for it to go away.我要怎么做才能让它远离 go。 The backend is up and running and I can see the static data but it wont show on the app后端已启动并正在运行,我可以看到 static 数据,但它不会显示在应用程序上

 import { makeStyles } from '@mui/styles' import React from 'react' import { Drawer } from '@mui/material' import { Typography } from '@mui/material' import List from '@mui/material/List' import ListItem from '@mui/material/ListItem' import ListItemIcon from '@mui/material/ListItemIcon' import ListItemText from '@mui/material/ListItemText' import { AddCircleOutlineOutlined, SubjectOutlined } from '@mui/icons-material' import { useHistory, useLocation } from 'react-router-dom' import AppBar from '@mui/material/AppBar' import Toolbar from '@mui/material/Toolbar' import { format } from 'date-fns' import { red } from '@mui/material/colors' const drawerWidth = 240 // 500 - subtract this number from const useStyles = makeStyles((theme) => { return{ page: { background: '#E5E4E2', width: '100%', padding: theme.spacing(3) }, drawer: { width: drawerWidth }, drawerPaper: { width: drawerWidth }, root: { display: 'flex' //places the drawer side by side with the page content }, active: { background: '#E5E4E2' }, // title:{ // padding: theme.spacing(13), // alignItems: 'center' // }, }}) export default function Layout({ children }) { const classes = useStyles() const history = useHistory() const location = useLocation() const menuItems = [ { text: 'My Projects', icon: <SubjectOutlined color="secondary" />, path: '/' }, { text: 'Create Project', icon: <AddCircleOutlineOutlined color="secondary" />, path: '/create' } ] return ( <div className={classes.root}> {/* side drawer */} <Drawer className={classes.drawer} variant='permanent' //Lets MUI know we want it on the page permanently anchor="left" // position of drawer classes={{ paper: classes.drawerPaper}} > <div> <Typography variant="h5" sx={{textAlign: 'center'}}> Projects </Typography> </div> {/* List / Links */} <List> {menuItems.map(item => ( <div className={location.pathname == item.path? classes.active: null}> <ListItem key={item.text} button onClick={() => history.push(item.path)}> <ListItemIcon>{item.icon}</ListItemIcon> <ListItemText primary={item.text} /> </ListItem> </div> ))} </List> </Drawer> <div className={classes.page}> <div className={classes.toolbar}></div> {children} </div> </div> ) }

enter image description here在此处输入图像描述

Updated更新

I'm sorry, of course, you should just move key to the parent div.对不起,当然,您应该将键移至父 div。 I didn't notice it.我没注意到。 Chris who answered in the comments is right and my answer was not needed.在评论中回答的克里斯是对的,不需要我的回答。 I rewrote the answer.我重写了答案。

To have an unique key use index in map or like you did item.text if text is unique for each element in map.要在 map 中拥有唯一的键使用索引,或者如果 map 中的每个元素的文本都是唯一的,那么就像您所做的 item.text 一样。

menuItems.map((item,index) =>

The idea is that map has to contain unique key for each element.这个想法是 map 必须包含每个元素的唯一键。 In result we have:结果我们有:

<div key={item.text} className={location.pathname == item.path ? classes.active : null}>

or或者

<div key={index} className={location.pathname == item.path ? classes.active : null}>

And you need to remove key from the List.您需要从列表中删除密钥。

Hope this helps, Regards,希望这会有所帮助,问候,

Your list item key in您的列表项键入

ListItem key={item.text}

is not unique.不是唯一的。

You have to change the value of the key so that it won't be duplicate with any other list item.您必须更改键的值,以便它不会与任何其他列表项重复。

暂无
暂无

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

相关问题 收到警告“警告:列表中的每个孩子都应该有一个唯一的”key“道具。” 当我的组件渲染时 - Getting warning “Warning: Each child in a list should have a unique ”key“ prop.” when my component render 警告:列表中的每个孩子都应该有一个唯一的“key”道具。 即使已经设置了密钥 - Warning: Each child in a list should have a unique "key" prop. Even after setting the key already 正在渲染对象数组的数组,不断得到“警告:列表中的每个孩子都应该有一个唯一的“键”道具。 - Was rendering arrays of arrays of objects, keep getting “Warning: Each child in a list should have a unique ”key“ prop.” 警告:列表中的每个孩子都应该有一个唯一的“关键”道具。 检查 `RenderComments` 的渲染方法 - Warning: Each child in a list should have a unique "key" prop. Check the render method of `RenderComments` 警告:列表中的每个孩子都应该有一个唯一的“关键”道具。 mapStateToProps - Warning: Each child in a list should have a unique "key" prop. mapStateToProps 警告:列表中的每个孩子都应该有一个唯一的“key”道具。 - ReactJS - Warning: Each child in a list should have a unique "key" prop. - ReactJS React.js,列表中的每个孩子都应该有一个唯一的“关键”道具。 如果已经给出密钥,如何解决 - React.js, Each child in a list should have a unique "key" prop. how to fix if key is already given 警告:列表中的每个孩子都应该有一个唯一的“关键”道具。 检查元素时不显示关键道具 - Warning: Each child in a list should have a unique “key” prop. Key prop doesn't show when inspect element 警告:数组或迭代器中的每个子代都应具有唯一的“键”道具。 li中已添加的关键道具 - Warning: Each child in an array or iterator should have a unique “key” prop.; key prop already added in li 警告:数组或迭代器中的每个子代均应具有唯一的“键”道具。 钥匙已经存在 - Warning: Each child in an array or iterator should have a unique “key” prop. Keys are already present
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM