如何在 JSX 中正确渲染 html?

问题描述

我有组件,我有来自 props 的带有 html 的字符串变量(例如):

<b style="font-weight: bold; font-size: 18px;">Lorem ipsum</b><span style="font-size: 18px;">&nbsp;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>
  )
}

是否有另一种方法可以在没有警告的情况下从 props 渲染 html 字符串?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)