我希望能够传递带有 HTML 标签的文本,如下所示:
<MyComponent text="This is <strong>not</strong> working." />
但是在 MyComponent
的 render 方法中,当我打印出 this.props.text
时,它实际上打印出了所有内容:
This is <strong>not</strong> working.
有什么方法可以让 React 解析 HTML 并正确转储它吗?
请您参考如下方法:
您可以使用带有字符串和 JSX 元素的混合数组(请参阅文档 here ):
<MyComponent text={["This is ", <strong>not</strong>, "working."]} />
这里有一个 fiddle 显示它正在工作:http://jsfiddle.net/7s7dee6L/
另外,作为最后的手段,你总是有 the ability to insert raw HTML但要小心,因为如果不清理属性值,这可能会让您遭受跨站点脚本 (XSS) 攻击。