0

I have created a component in a react js app, which calls an api using fetch and get some json data like below. How can i display the html in the json data as is, in my own html code?

   data = {
              duration : "1", 
              html : "<div><p>some message here</p><p> more text here <span> styled text <span><p></div>"
          }
3
  • 1
    Do you mean you want to display the html as raw text? Or you want to render the html? Commented Dec 17, 2020 at 14:29
  • i want to render html Commented Dec 17, 2020 at 14:32
  • Maybe this can work. $("body").append($(data.html)); If jquery works. Commented Dec 17, 2020 at 14:34

1 Answer 1

5

First you should use a package html-react-parser

npm i html-react-parser

then

import parse from 'html-react-parser'
const yourHtmlString = '<h1 >Hello</h1>' // it's mean your data state html field

after just call like this

 <div>
    {parse(yourHtmlString)}
</div>

Also you can find more detail about this in this question

Render HTML string as real HTML in a React component

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.