reactjs - 尽管添加了唯一键,但反应还是会发出警告
问题描述
尽管向元素key={item.?key}添加了唯一键,但我仍然收到以下警告:
警告:列表中的每个孩子都应该有一个唯一的“关键”道具。
使用 . 检查顶级渲染调用<ol>
。
import React from 'react';
function myComponent(...data) {
return (
<section className="my-component">
<ol className="my-component__cards">
{data[0].items.map((item) => (
<a key={item?.key} className="my-component__slide">
<li>
<h3>{item?.title?.rendered}</h3>
<p dangerouslySetInnerHTML={{ __html: item?.content?.rendered }} />
</li>
</a>
))}
</ol>
</section>
);
}
export default myComponent;
谢谢
解决方案
如果您item
有一个名为 as 的唯一道具key
,那么这必须很好,您需要替换?
为!
. (打字稿问题,打字稿解决方法:D)但如果您不确定,只需index
用作关键道具。
{data[0].items.map((item, index) => (
<a key={index} ...
推荐阅读
- python - Python TPL 数据流模拟
- aem - 将数字签名字段设为只读
- c - 如何在C中合并文本中的所有可能空格
- java - 如何使用 SNMP 从 UPS 设备获取和设置值?
- javascript - MongoDB返回错误的行
- python - 如何使用 python 烧瓶 API 验证客户端
- c++ - 为什么我不能使用 const 指定的元素类型对对进行排序?
- php - 从 laravel 中的 hasManyThrough 关系中获取 id
- android - Flutter Android 无法构建 - Gradle 任务 assembleDebug 失败,退出代码为 1
- firebase - 如何使用本机图像的url删除firebase存储中的图像?