亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何在我的 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 關注
  • 157 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號