简体   繁体   中英

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

<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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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