简体   繁体   English

React.js 将文本呈现为 HTML

[英]React.js render text as HTML

This is how the render code looks now (And I know it's unsafe to do so):这就是渲染代码现在的样子(我知道这样做是不安全的):

render: function() {
  return (
    <div className="container-fluid pages_container">
      <p dangerouslySetInnerHTML={{__html: this.state.page.body}} />
    </div>
  );
}

The question is how can I render it safely?问题是我怎样才能安全地渲染它?

Reference https://facebook.github.io/react/docs/dom-elements.html#dangerouslysetinnerhtml参考https://facebook.github.io/react/docs/dom-elements.html#dangerouslysetinnerhtml

<script src="https://cdnjs.cloudflare.com/ajax/libs/marked/0.3.2/marked.min.js"></script>

render: function() {
  return (
    <div className="container-fluid pages_container">
      <p dangerouslySetInnerHTML={{__html: marked(this.state.page.body, {sanitize: true})}} />
    </div>
  );
}

jsFiddle js小提琴

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM