微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何在 JSX 中正确渲染 html?

如何解决如何在 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 举报,一经查实,本站将立刻删除。