【问题标题】:html string to html (react front-end)html 字符串到 html(反应前端)
【发布时间】:2019-03-25 03:43:51
【问题描述】:

这是我想使用 reactDOM 渲染的字符串:

<!DOCTYPE html><html><head><title>Websockets 101</title><meta charset="utf-8"><link rel="shortcut icon" href="#"><link rel="stylesheet" href=
"https://fonts.googleapis.com/css?family=Roboto"><link rel="stylesheet" href="../stylesheets/styles.css" type="text/css"><link rel="stylesheet" href
="../stylesheets/projects_showcase.css" type="text/css"><link rel="stylesheet" href="../stylesheets/portfolio/portfolio.css" type="text/css"><!-- Bo
otstrap--><!--link(rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css")--></head><body id="portfolio"></bo
dy></html><div><h1>This is the story of a bread named brown</h1><p>bread was brown and was also named brown</p><p>thus he was brown</p></div>

我尝试了以下方法:(仅显示渲染方法)其中this.state.note_html 是 html 文档字符串

        return (
                <div style={{background: 'red', width: '300px', height: '300px'}}>
                    <h1>Hello</h1>
                    <button onClick={this.fetchHTML}>Click me</button>
                    {this.state.note_html}
                </div>

我没有工作。它只是把它渲染成一个字符串。

结果:

【问题讨论】:

    标签: html node.js reactjs


    【解决方案1】:

    试试这个:

    return (
       <div style={{background: 'red', width: '300px', height: '300px'}}>
          <h1>Hello</h1>
          <button onClick={this.fetchHTML}>Click me</button>
          <div dangerouslySetInnerHTML={this.createMarkup()} />;
       </div> );
    ...
    createMarkup() {
      return {__html: this.state.note_html};
    }
    

    【讨论】:

    • 您可以手动将this.state.note_html 替换为问题中的 HTML 字符串,看看它是否可以这样工作?
    • 我很抱歉您的回答有效。我忘了用 webpack 重建我的包。我不知道谁对你投了反对票,但我只是对你投了赞成票。
    猜你喜欢
    • 2016-08-20
    • 2017-09-05
    • 1970-01-01
    • 1970-01-01
    • 2020-05-15
    • 1970-01-01
    • 2011-10-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多