19
var Klass = React.createClass({
  this.props.html_string = '<button>BUTTON_TEXT</button>';
  render: function(){
    return (
      <div className="wrapper">
        {this.props.html_string}
      </div>
    );
  }
});

Currently {this.props.html_string} gives me a text node. How can I make this a HTML DOM node?

Derek
  • 11,980
  • 26
  • 103
  • 162

2 Answers2

39

What you want is dangerouslySetInnerHTML

https://facebook.github.io/react/tips/dangerously-set-inner-html.html

function createMarkup() { return {__html: 'First &middot; Second'}; };
<div dangerouslySetInnerHTML={createMarkup()} />

Edit: you'll want to htmlencode the string.

SnareHanger
  • 928
  • 11
  • 22
5

Also be aware of React.renderToString('html') which can be used on server - https://facebook.github.io/react/docs/top-level-api.html

Kosmetika
  • 20,774
  • 37
  • 108
  • 172
  • 9
    `React.renderToString()` is now deprecated, use `ReactDOMServer.renderToString()` now (`import ReactDOMServer from 'react-dom/server'`) – skplunkerin Jan 31 '17 at 20:18