首页 > 解决方案 > 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

提前致谢

标签: reactjs

解决方案


在 React 16 之后,您可以使用<Fragment>.

只需以这种方式发送道具,它就会正常工作:

<MyComponent text={<Fragment>"This is <strong>not</strong> working."</Fragment>]} />

推荐阅读