我有组件,并且我有来自 props 的带有 html 的字符串变量(例如):
<b style="font-weight: bold; font-size: 18px;">Lorem ipsum</b><span style="font-size: 18px;"> dolor sit amet</span>
我如何在 JSX 中呈现 html 对吗?
const Item: React.FC<ItemProps> = (props) => {
const { item } = props;
const { title, htmlText} = item;
return (
<div className="item">
<div>{title}</div>
<div>{htmlText}</div>
// it render it as plain text
<div dangerouslySetInnerHTML={{ __html: htmlText }}></div>
// it render htmlText as html but I got react warning -
// "Dangerous property 'dangerouslySetInnerHTML' found react/no-danger"!
</div>
)
}
是否有另一种方法可以在没有警告的情况下从道具呈现 html 字符串?