Printing data HTML with React

18,441

Try using dangerouslySetInnerHTML

<article>
  <h2><a href={post.link}>{post.title.rendered}</a></h2>
  <div className="post__content" dangerouslySetInnerHTML={{__html: post.content.rendered}}></div>
</article>
Share:
18,441

Related videos on Youtube

Tomek Buszewski
Author by

Tomek Buszewski

Updated on June 04, 2022

Comments

  • Tomek Buszewski
    Tomek Buszewski almost 2 years

    I have a JSON table with posts. Content is delivered in HTML, and when I try to render it in React:

      return (
        <article>
          <h2><a href={post.link}>{post.title.rendered}</a></h2>
          <div className="post__content">{post.content.rendered}</div>
        </article>
      )
    

    I am getting full HTML markup printed (escaped). What should I do?

  • Tomek Buszewski
    Tomek Buszewski about 8 years
    I know about this method, but isn't it dangerous? On the other hand, there won't be any user-written posts.
  • james emanon
    james emanon about 8 years
    I've always used it, but I've always had control or knew the origin of the date I was rendering. So, yeah - its a touch choice. My thought outside of this would be to strip all html characters then deal with beautification on your end.