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