[英]Displaying pure html in react
I playing with react-draft-wysiwyg editor. 我在玩react-draft-wysiwyg编辑器。 I progress quite well.
我进步很好。 But now I stuck how can I display the output of the editor.
但是现在我卡住了如何显示编辑器的输出。
For example, let suppose body is the output of the wysiwyg editor: 例如,假设body是所见即所得编辑器的输出:
function ShowHtml(props) {
let body = '<p>Sample text with <strong>bold</strong> and <em>italic</em></p>'
return (
<div>
{body}
</div>
)
}
Now the output will be exactly the same html with tags displayed without formatting. 现在,输出将是完全相同的html,其中显示的标签没有经过格式化。
<p>Sample text with <strong>bold</strong> and <em>italic</em></p>
And I would like something like this: 我想要这样的事情:
Sample text with bold and italic
带有粗体和斜体的示例文本
In jQuery I would just set the html property of the div tag. 在jQuery中,我只是设置div标签的html属性。 But I do not know how to do it the proper way in React.
但是我不知道如何在React中正确地做到这一点。 Can I just grab a reference to the div and update it somehow just like in jQuery?
我可以像在jQuery中那样仅获取对div的引用并以某种方式对其进行更新吗? Is it working with the virtual Dom?
它与虚拟Dom一起使用吗?
try to insert as an attribute dangerouslySetInnerHTML={{__html: body}} 尝试以危险方式插入作为属性SetInnerHTML = {{____:body}}
function ShowHtml(props) {
let body = '<p>Sample text with <strong>bold</strong></p>'
return (
<div dangerouslySetInnerHTML={{__html: body}} />
)
}
https://reactjs.org/docs/dom-elements.html#dangerouslysetinnerhtml https://reactjs.org/docs/dom-elements.html#dangerouslysetinnerhtml
Try removing the double quote or if you wanna keep the double quote you can use dangerouslySetInnerHTML
尝试删除双引号,或者如果您想保留双引号,则可以
dangerouslySetInnerHTML
使用dangerouslySetInnerHTML
function ShowHtml(props) {
let body = '<p>Sample text with <strong>bold</strong> and <em>italic</em></p>'
return (
<div dangerouslySetInnerHTML={{__html: body}}/>
)
}
If you want to keep the double quotes, You can also setup the innerHTML dangerously: 如果要保留双引号,还可以危险地设置innerHTML:
function ShowHtml(props) {
let body = '<p>Sample text with <strong>bold</strong> and <em>italic</em></p>'
return (
<div dangerouslySetInnerHTML={{__html: body}}>
</div>
)
}
function ShowHtml() {
return (
<div>
<p>Sample text with <strong>bold</strong> and <em>italic</em></p>
</div>
)
}
You can use dangerouslySetInnerHTML
to render html inside react components: 您可以使用
dangerouslySetInnerHTML
渲染HTML里面反应的组分:
renderBodyContents = () => {
return {__html: this.state.body}
}
...
render(){
return(
<p> dangerouslySetInnerHTML={this.renderBodyContents()}></p>
)
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.