即使我放了一把 keys ,我还是收到了这个错误. REACT_DevTools_Backend.js:4026警告:列表中的每个子元素都应该有唯一的"key"props . Shows here a picture of the error

此处显示了代码:

  return (
<div>
  <div style={{ textAlign: "center" }}>
    {players?.length > 0 && (
      <div style={{ marginTop: "2rem" }}>
          <h2>Results "{query}"</h2>
          <TableContainer
            component={Paper}
            style={{
              marginLeft: "auto",
              marginRight: "auto",
              width: "90%",
            }}
          >
            <Table sx={{ minWidth: 350 }} aria-label="simple table">
              <TableHead>
                <TableRow>
                  <TableCell>Full Name</TableCell>
                  <TableCell>Club</TableCell>
                  <TableCell>Nation</TableCell>
                  <TableCell>Position</TableCell>
                  <TableCell>Age</TableCell>
                  <TableCell>Market Value</TableCell>
                  <TableCell>Favorite</TableCell>
                </TableRow>
              </TableHead>
              <TableBody>
                {players.map((item) => {
                  return <TableRow
                  key={item.id}
                  sx={{
                    "&:last-child td, &:last-child th": { border: 0 },
                  }}
                  style={{ textAlign: "center" }}
                >
                  <TableCell>
                    <ListItem>
                      <Link
                        onClick={() => pickPlayer(item.id)}
                        style={{ textDecoration: "none" }}
                        to={`/player/${item.id}`}
                      >
                        {item?.img !== "https://img.a.XXX.technology/portrait/header/default.jpg?lm=1" &&
                        <img
                        src={item?.img}
                        alt={item?.displayName}
                        style={{ marginRight: "5px" , width: '2.5rem' , borderRadius: '80%' }}
                      />
                        }
                        {item?.img === "https://img.a.XXX.technology/portrait/header/default.jpg?lm=1" &&
                        <img
                        src={'https://www.seekpng.com/png/full/202-2024994_profile-icon-profile-logo-no-background.png'}
                        alt={item?.displayName}
                        style={{ marginRight: "5px" , width: '3rem'}}
                      />
                        }
                        <span style={{ verticalAlign: "1rem" }}>
                          {item?.displayName}
                        </span>
                      </Link>
                    </ListItem>
                  </TableCell>
                  <TableCell>
                    <ListItem>
                      <img
                        src={`https://tmssl.akamaized.net/images/wappen/head/${item?.imgId}.png?lm=1457423031`}
                        alt={item?.club}
                        title={item?.club}
                        style={{ marginRight: "2px" ,  width: '1.7rem' }}
                      />{" "}
                      {item?.club}
                    </ListItem>
                  </TableCell>
                  <TableCell>
                    {item?.nationality.map((item) => {
                      return (
                        <>
                          <ListItem key={uuidv4()}>
                            <img
                              key={uuidv4()}
                              src={item?.flag}
                              alt={item?.title}
                              title={item?.title}
                              style={{ marginRight: "5px" , width : '1.7rem' , borderRadius : '10rem' }}
                            />{" "}
                            {item.title}
                          </ListItem>
                        </>
                      );
                    })}
                  </TableCell>
                  <TableCell>
                    <ListItem>{item.position}</ListItem>
                  </TableCell>
                  <TableCell>
                    <ListItem>{item.age}</ListItem>
                  </TableCell>
                  <TableCell>
                    <ListItem>{item.tmValue}</ListItem>
                  </TableCell>
                  <TableCell>
                    <ListItem>
               
                 {
                  favoriteLoading ? "loading..." :
                  <>
                  {
                    favorite.find(fav => fav.playerId === item.id && fav.scoutId === auth._id) !== undefined ? <Favorite style={{ color: "red" }} /> : <FavoriteBorder style={{ color: "red" }} />
                  }
                  </>
                  }
                    </ListItem>
                  </TableCell>
                </TableRow>
                }
                )}
              </TableBody>
            </Table>
          </TableContainer>
          </div>
      )}
      {playersLoading && players?.length === 0 ? progress : players?.length === 0 && <h3>{query} Not Found</h3> }
      {playersLoading && players?.length > 0 && progress}
      {playersLoading && players?.length === 0 ? null : players.length <= 3 && <div style={{height : "20vh"}}>
      {
        !underThree && <h4>Scroll Down to load more</h4>
      }
      </div>}
  </div>
</div>
);

我们能做些什么? 到目前为止,我甚至try 使用外部动态UUID来判断ID是否重复,但事实并非如此. 因为在我看来一切都很好.

推荐答案

问题出在以下代码中:

{item?.nationality.map((item) => {
  return (
    <>
      <ListItem key={uuidv4()}>
        <img
          key={uuidv4()}
          src={item?.flag}
          alt={item?.title}
          title={item?.title}
          style={{ marginRight: "5px" , width : '1.7rem' , borderRadius : '10rem' }}
        />{" "}
        {item.title}
      </ListItem>
    </>
  );
})}

键必须位于最外层的元素上.您最外层的元素是一个片段<>,它没有键.如果您不需要该片段,可以将其删除,并将ListItem作为最外层的元素.

如果您确实需要在外部有一个片段,您可以使用片段的手写形式为其添加一个键:

import { Fragment } from 'react';
// ...
return (
  <Fragment key={/* insert key here */}> 
    <ListItem>
      <img
        src={item?.flag}
        alt={item?.title}
        title={item?.title}
        style={{ marginRight: "5px" , width : '1.7rem' , borderRadius : '10rem' }}
      />{" "}
      {item.title}
    </ListItem>
  </Fragment>
)

顺便说一句,就像你对关键点所做的那样,调用uuidv4()将在每次渲染时创建一个全新的关键点.这将强制每个列表项在每次渲染时卸载并重新挂载.理想情况下,密钥应该是item上的一条数据.如果这不是一种 Select ,那么索引可以作为最后的手段使用.但不要使用在每次渲染时都会更改的随机关键点.

Reactjs相关问答推荐

Next.js-图像组件加载问题

使用Thattle和Use Effect setTimeout进行搜索有什么不同

使用`Link`包装`tr`标记会在Next.js中产生水合错误14

如何在NextJS中仅在刷新页面和页面淡出页面过渡时添加全屏加载器

试图从Amazon-Cogito-Identity-js模拟CogitoUser.authateUser,并将其获取为未定义的S

MUI:在props 主题中找到的值:;错误;是[Object],而不是字符串或数字

列表中的每个子项都应该有一个唯一的keyprops .在react 应用程序中

对于 ref 上的可见 prop 或 open 方法来react 组件,哪种方式更好?

useState数组不更新

无法使axiosmockadapter正常工作

从 React 应用程序调用未经身份验证的 graphql 查询时出现错误:没有当前用户

如何在悬停时更改 MUI 卡内容

我正在try 将一组加载程序函数发送到我的路由,它抛出一个错误 Handler is not a funtion in a reactJs application

如何在不重新加载页面的情况下加载新添加的数据?

如何在 ChartJS 中操作 Y 轴

作为单个变量的 React 组件是否比 memoized 组件渲染得更快?

错误:无效挂钩调用.钩子只能在函数组件的主体内部调用.使用 next/router 时

如果 URL 已随功能组件更改,则取消刷新

Material UI 安装和 React v. 18.2 出现问题

Map 函数只返回 Array 中的图像