1

I have a JSON file with a variable called htmlContent. I am trying to display this in a component but when it is rendered it shows it as text. I just want the text to be surrounded by an h2 tag.

MY JSON FILE

const BookData = {
  data: [
    {
      id:"1",
      pageHeader:"Contents",
      htmlContent:`<h2>hello</h2>`,
      definePrototypes:"",
      exportComponent:"export default App;"
    }
  ]

};

MY REACT COMPONENT

      <section id='content'>
          {props.htmlContent }
          </section>

MY ACTUAL RESULT <h2>hello</h2>

MY EXPECTED RESULT hello

1
  • <section id='content'><h2>{props.htmlContent }</h2></section> ? Why is the <h2> inside the string and not the JSX unless you want to render the string <h2> and not an <h2> element. Commented Jan 22, 2020 at 9:43

3 Answers 3

3

You need to use dangerouslySetInnerHTML to render html content in react, otherwise it will show as string

Change to be made in this element

<section id='content'>
    {props.htmlContent }
</section>

Change the above block to

<section id='content' dangerouslySetInnerHTML={{ __html: props.htmlContent }}></section>

This should do the job. Check the link for details https://reactjs.org/docs/dom-elements.html#dangerouslysetinnerhtml

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

3 Comments

Yes, that is correct. But better to return it as a simple text and wrap that property in <h2> yourself.
@AbdulMannan, thats also correct, but his need is to show html content in dom, for that there is only one proposed method, that is dangerouslySetInnerHTML
Exactly. That's why I mentioned Yes, that is correct. However, its always good to share the best practice if that is doable.
0

Try this

<div contentEditable='true' dangerouslySetInnerHTML={{ __html: props.htmlContent }}></div>

The immediate effect of using innerHTML versus dangerouslySetInnerHTML is identical -- the DOM node will update with the injected HTML.

However, behind the scenes when you use dangerouslySetInnerHTML it lets React know that the HTML inside of that component is not something it cares about.

Because React uses a virtual DOM, when it goes to compare the diff against the actual DOM, it can straight up bypass checking the children of that node because it knows the HTML is coming from another source. So there's performance gains.

More importantly- if you simply use innerHTML, React has no way to know the DOM node has been modified. The next time the render function is called, React will overwrite the content that was manually injected with what it thinks the correct state of that DOM node should be.

Your solution to use componentDidUpdate to always ensure the content is in sync I believe would work but there might be a flash during each render.

Reference Dangerously Set innerHTML

Comments

0

If React is imported in the JS file you can just remove the ticks(`) from the JSON like this:

 const BookData = {
  data: [
    {
      id:"1",
      pageHeader:"Contents",
      htmlContent:<h2>hello</h2>,
      definePrototypes:"",
      exportComponent:"export default App;"
    }
  ]

};

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.