React.js将文本呈现为HTML [英] React.js render text as HTML
本文介绍了React.js将文本呈现为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>
);
}
问题是如何安全地渲染它?我知道jQuery中有一些.html()方法,但我没有做到:)
The question is how can I render it safely? I know there's some .html() method in jQuery, but I didn't make it :)
P.S。包含jQuery,因为它是一个Rails应用程序。
P.S. jQuery is included since it's a Rails application.
推荐答案
参考 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>
);
}
这篇关于React.js将文本呈现为HTML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文