[英]How to convert string to jsx?
How can I convert this string to jsx? 如何将该字符串转换为jsx? I want this string to appear as an HTML element.
我希望此字符串显示为HTML元素。
this.setState({
input: "<h1 id="heading">Heading</h1> <h2 id="sub-heading">Sub-heading</h2> <h3 id="another-deeper-heading">Another deeper heading</h3>"
});
render() {
return <div>{this.state.input}</div>
}
You don't need to quote HTML elements in JSX, but an HTML element can only have one parent. 您无需在JSX中引用HTML元素,但是HTML元素只能有一个父元素。 Get rid of the quotes, and move the div up to the variable, and you'll be all set.
删除引号,然后将div移至变量,即可完成所有设置。
So: 所以:
this.setState({
input: <div><h1 id="heading">Heading</h1> <h2 id="sub-heading">Sub-heading</h2> <h3 id="another-deeper-heading">Another deeper heading</h3></div>
});
render() {
return this.state.input
}
I'm not sure what you gain from this, though. 不过,我不确定您将从中获得什么。 Best practice would be to keep simple data in
props
and state
, and put the HTML wrapper in the render function, not in state
. 最佳实践是将简单数据保留在
props
和state
,并将HTML包装器置于render函数中,而不是state
。
Just to highlight the solution, the comment by @user633183: 为了突出解决方案,@ user633183的评论:
Use dangerouslySetInnerHTML
dangerouslySetInnerHTML
使用dangerouslySetInnerHTML
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.