提问人:Pat 提问时间:9/18/2023 最后编辑:Taiwei TuanPat 更新时间:9/26/2023 访问量:29
使用 React Material UI 将明细线分布在 2 行上 表格给出警告“警告:列表中的每个子项都应该有一个唯一的”键“道具。[复制]
Spread a detail line over 2 rows using React Material UI Table gives warning "Warning: Each child in a list should have a unique "key" prop." [duplicate]
问:
我正在尝试将我的表格格式化为:
EVENT ID EVENT NAME DATE LOCATION
12345678 Rolling Stones 01/01/2024 Merriweather Post Pavillion
Rain or shine! No weapons or alcohol will be permitted
12345679 Exorcist Believer 01/01/2024 Regal Cinemas
No one under the age of 18 will be admitted without a parent / legal guardian
12345680 Justin Beiber 01/01/2024 Regal Cinemas
All ages - 12 and up, no outside food or drinks permitted
虽然它显示良好,但我收到警告“警告:列表中的每个子项都应该有一个唯一的”键“道具。看起来 Material UI 表将每个 TableRow 解释为不同的行,而不是将它们评估为单个逻辑行并为两者分配相同的键值,这会导致重复键警告。
有谁知道如何摆脱警告?
我的代码是:
<Paper className="paper">
<TableContainer className="table-container">
<Table>
<TableHead>
<TableRow>
<TableCell>
Event ID
</TableCell>
<TableCell>
Event Name
</TableCell>
<TableCell>
Date
</TableCell>
<TableCell>
Location
</TableCell>
</TableRow>
</TableHead>
<TableBody>
{rows && rows.map((row) => {
return (
<>
<TableRow key={row.eventId}>
<TableCell>
<row.eventId}
</TableCell>
<TableCell>
<row.eventName}
</TableCell>
<TableCell>
<row.eventDate}
</TableCell>
<TableCell>
<row.location}
</TableCell>
</TableRow>
<TableRow>
<TableCell colspan={4}>
<row.description}
</TableCell>
</TableRow>
</>
);
})}
</TableBody>
</Table>
<TableContainer>
</Paper>
答:
1赞
Pat
9/18/2023
#1
我找到了答案:
警告:列表中的每个子项都应该有一个唯一的“键”道具,但我有键道具
我需要将 key 属性传递给 Fragment 标签,而不是 TableRow 标签。
它现在可以工作了。
评论
0赞
Harrison
9/19/2023
如果这是重复的问题,则应将其标记为重复,而不是将答案作为链接发布
评论