Javascript React.js 将文本呈现为 HTML
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31850614/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 07:16:05 来源:igfitidea点击:
React.js render text as HTML
提问by Fleeck
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?
问题是我怎样才能安全地渲染它?
回答by Vishnu
Reference https://facebook.github.io/react/docs/dom-elements.html#dangerouslysetinnerhtml
参考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>
);
}