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,588
  • 25
  • 97
  • 153

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
  • 919
  • 14
  • 22
4

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,095
  • 37
  • 101
  • 164
  • 8
    `React.renderToString()` is now deprecated, use `ReactDOMServer.renderToString()` now (`import ReactDOMServer from 'react-dom/server'`) – skplunkerin Jan 31 '17 at 20:18