首页 > 解决方案 > html 渲染没有发生

问题描述

我从后端收到 json 响应,例如:

{'status': 0, 'type': 'BASIC_HTML', 'content': '<h1>Hello World </h1>'}

我将 html 响应呈现为:

if (props.text.type == "BASIC_HTML") {
    return (
        <div>
            {props.text.content}
        </div>
    )
}

但是输出为<h1>Hello World </h1>

它不是以 html 形式出现的。请帮忙。

标签: reactjsredux-saga

解决方案


JSX 不是由字符串组成的。

您的数据应该只包含您希望打印的句子。我建议更改您的后端配置以返回以下内容:

{'status': 0, 'type': 'BASIC_HTML', 'content': 'Hello World'}

并且<h1>节点应该写成 JSX,而不是字符串:

if (props.text.type == "BASIC_HTML") {
    return (
        <div>
            <h1>{props.text.content}</h1>
        </div>
    )
}

推荐阅读