You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

React列表缺唯一key属性警告及用户列表表格不渲染问题求助

解决表格数据不显示及React key警告问题

你的代码存在两个问题,导致数据无法渲染且触发警告,修正后的代码如下:

<TableBody>
  {users.map(user => (
    <TableRow key={user._id}>
      <TableCell>{user._id}</TableCell>
      <TableCell>{user.name}</TableCell>
      <TableCell>{user.username}</TableCell>
      <TableCell>{user.email}</TableCell>
      <TableCell>{user.phone}</TableCell>
    </TableRow>
  ))}
</TableBody>

错误说明:

  • 数据访问错误:在map循环中,你错误地使用了数组users来访问属性(如users._id),但实际上应该使用当前迭代的对象user,否则无法获取到单个用户的具体数据。
  • 缺少唯一key属性:React要求列表中的每个子组件必须拥有唯一的key prop,这是React diff算法识别元素的关键。这里使用用户的_id作为key,因为它是每个用户的唯一标识。

内容的提问来源于stack exchange,提问作者user19653468

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.24 15:54:23