简体   繁体   English

如何在React中呈现HTML实体而不危险地设置InnerHTML?

[英]How to render HTML entity in React without dangerouslySetInnerHTML?

I'm using custom fonts in my site, similar to Font Awesome, which have simple signature: 我在网站中使用自定义字体,类似于Font Awesome,它们具有简单的签名:

<i className="icon-plus"></i>

But I want to create own component which will be render dynamic HTML entities like: 但是我想创建自己的组件,该组件将呈现动态HTML实体,例如:

const iconEntity = '&#xf2b9;' // (Font Awesome HTML entity example)
const icon1 = '&#xf13d;' // dynamic entities

class OwnIcon extends React.Component {
  render() {
   return <i>{iconEntity}</i>
  }
}

But this doesn't work. 但这是行不通的。 After reading this post I trier using dangerouslySetInnerHTML and it works, eg: 看完这篇文章后,我会使用dangerouslySetInnerHTML使用SetInnerHTML进行dangerouslySetInnerHTML并且可以正常工作,例如:

const iconEntity = '&#xf2b9;'

class OwnIcon extends React.Component {
  render() {
    return <i className="icon" dangerouslySetInnerHTML={{__html: iconEntity }}></i>
  }
}

But it's a little bit ugly solution. 但这是一个难看的解决方案。 After reading JSX gotchas I found that there are solutions like: 阅读JSX陷阱后,我发现有一些解决方案,例如:

A safer alternative is to find the unicode number corresponding to the entity and use it inside of a JavaScript string:

<div>{'First \u00b7 Second'}</div>
<div>{'First ' + String.fromCharCode(183) + ' Second'}</div>

But how I could convert(for example, Font Awesome) Unicode Private-Use characters "\\f007" or HTML hex entity &#xf007 to get unicode number to get rid of dangerouslySetInnerHTML and render icons in more "clear" way? 但是,如何转换(例如Font Awesome)Unicode专用字符"\\f007"或HTML十六进制实体&#xf007来获得unicode号以摆脱dangerouslySetInnerHTML &#xf007并以更“清晰”的方式呈现图标?

Text examples: here 文字范例: 这里

Put it in a Fragment so the value becomes JSX, and will be output as HTML: 将其放在Fragment以便该值变为JSX,并将其输出为HTML:

const iconEntity = <Fragment>&#xf2b9;</Fragment>

class OwnIcon extends React.Component {
  render() {
    return <i className="icon">{iconEntity}</i>
  }
}

The downside to this, of course, is that you don't get to say "Let's get dangerous!" 当然,这样做的缺点是您不会说“让我们变得危险!” like Darkwing Duck. 像黑翼鸭一样。 ;) ;)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 是否可以在不使用 dangerouslySetInnerHtml 的情况下渲染 HTML - Is it possible to render HTML in react without using dangerouslySetInnerHtml 反应使用危险的SetInnerHTML 渲染带有 html 标签的 json - react use dangerouslySetInnerHTML to render json with html tags 设置危险地设置innerHtml时,如何从ReactJS中的html字符串呈现react组件? - How to render a react component from html string in ReactJS while setting in dangerouslysetinnerHtml? 在React中危险地渲染带有JSX表达式的HTML字符串SetInnerHTML() - Render HTML string w/JSX expression in React dangerouslySetInnerHTML() 用React的危险地设置HTML到SetInnerHTML - draft-to-html with React's dangerouslySetInnerHTML 在React中,SetInnerHTML不适用于html标签。 - In React dangerouslySetInnerHTML is not working for html tags. 使用危险的SetInnerHTML在React中插入完整的HTML - Inserting complete HTML in React using dangerouslySetInnerHTML 在后台从 React 组件制作 HTML 字符串,如何在另一个 React 组件中通过危险的SetInnerHTML 使用该字符串 - Making an HTML string from a React component in background, how to use the string by dangerouslySetInnerHTML in another React component 如何在没有 html 标记的情况下呈现 React Quill 的内容? - How to render the content of React Quill without the html markup? 如何在不使用 Webview 的情况下在 React Native 中渲染多个 HTML 内容? - How to render multiple HTML content in React Native without using Webview?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM