reactjs - Rect - 在 props 字符串中传递 HTML 标签 - 缺少“key”值
问题描述
据此,我正在使用此方法将简短的 HTML 代码作为字符串传递给道具:
<MyComponent text={["This is ", <strong>not</strong>, "working."]} />
它工作正常......几乎没问题。
React 给出了这个警告:
Warning: Each child in a list should have a unique "key" prop.
Check the render method of `MyComponent`. It was passed a child from TheParent. See https://reactjs.org/link/warning-keys for more information.
at strong
有什么方法可以通过这种方式传递 html,并以某种方式输入密钥?我想避免使用dangerouslySetInnerHTML
提前致谢
解决方案
在 React 16 之后,您可以使用<Fragment>
.
只需以这种方式发送道具,它就会正常工作:
<MyComponent text={<Fragment>"This is <strong>not</strong> working."</Fragment>]} />
推荐阅读
- mysql - 如何将表 2 中的一些值和一些文字值插入到 table1 中?
- java - PDFBOX 2.0.18 - 如何遍历 PDF 页面并检索特定字段
- r - 为什么 `select_if(., is_double)` 返回日期?
- sql - 如何在 SQL 中将具有成对关系的行合并为单行
- python-3.x - 计算多个城市的中心点纬度/经度
- javascript - 如何使用 eventListener 通过 Node 和 Express 发送发布请求?
- python - QTreeView 只装饰和缩进某些列
- javascript - 如何在 TypeScript 中安全地键入分层元数据对象?
- vuex - 更新过滤的多行 Vuex 支持的表单会导致“不改变 vuex 存储状态”
- php - csv 文件的每一行都有州、县和数据。如何使用 PHP 关联数组转换为 states=>counties=>county=>data