reactjs - 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 形式出现的。请帮忙。
解决方案
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>
)
}
推荐阅读
- class - Kotlin – 为什么必须在子类中传递参数值?
- c++ - 如何使用字符串或字符向量(分别包含任何化学成分公式)并计算其摩尔质量?
- javascript - 单独的日志级别
- python-3.x - 通过将鼠标悬停在图形上来打印图形名称
- python - Discord.py client.say 在事件循环任务中不起作用
- sql - 删除时触发以避免悬空引用
- java - 将 jBox2D 添加到 Android Studio 项目
- css - 如何为圆形图像创建透明的内边框?
- python - 图像阈值处理无法完全去除图像中的点状噪声
- jquery - 示例之间有什么区别:div.panel.panel-default 和 .panel.panel-default?