如何使用html字符串渲染react元素? [英] How to render a react element using an html string?

查看:129
本文介绍了如何使用html字符串渲染react元素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

目前 {this.props.html_string} 给了我一个文本节点。如何将其设为HTML DOM节点?

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

推荐答案

您想要的是 dangerouslySetInnerHTML

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

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

编辑:你想要htmlencode字符串。

you'll want to htmlencode the string.

这篇关于如何使用html字符串渲染react元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆