在 jsx 中 渲染 html 元素
在上半部分标签中使用 dangerouslySetInnerHTML={{ __html: this.state.属性名 }}
import React from "react";
class Component1 extends React.Component {
constructor(props) {
super(props);
this.state = {
htmlContent: "<h1>我是html元素</h1>",
};
}
render() {
return (
<div>
<div>{this.state.htmlContent}</div>
<div dangerouslySetInnerHTML={{ __html: this.state.htmlContent }}></div>
</div>
);
}
}
function App() {
return (
<div>
<Component1 />
</div>
);
}
export default App;
网友评论