美文网首页React
react 在模板中渲染 html 字符串

react 在模板中渲染 html 字符串

作者: 暴躁程序员 | 来源:发表于2022-04-05 08:17 被阅读0次

    在 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;
    

    相关文章

      网友评论

        本文标题:react 在模板中渲染 html 字符串

        本文链接:https://www.haomeiwen.com/subject/ztztsrtx.html