为了账号安全,请及时绑定邮箱和手机立即绑定

如何在我的 React js 页面中显示来自 json 数据的 html 代码?

如何在我的 React js 页面中显示来自 json 数据的 html 代码?

哆啦的时光机 2023-12-14 15:32:20
我在 React js 应用程序中创建了一个组件,它使用 fetch 调用 api 并获取一些 json 数据,如下所示。我怎样才能在我自己的html代码中按原样显示json数据中的html?   data = {              duration : "1",               html : "<div><p>some message here</p><p> more text here <span> styled text <span><p></div>"          }
查看完整描述

1 回答

?
牧羊人nacy

TA贡献1862条经验 获得超7个赞

首先你应该使用一个包 html-react-parser


npm i html-react-parser

然后


import parse from 'html-react-parser'

const yourHtmlString = '<h1 >Hello</h1>' // it's mean your data state html field

像这样打电话之后


 <div>

    {parse(yourHtmlString)}

</div>

查看完整回答
反对 回复 2023-12-14
  • 1 回答
  • 0 关注
  • 47 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信