Suanmeiguo
Suanmeiguo

Reputation: 1405

React: how do I render source code in a variable?

I store some html source code in database, it's a block of code like this:

<div class="content"><h1>Page 1</h1></div>

Now in react I get them in a variable ({this.state.content}), but when I try to render them the source code got displayed as a string. Here's how I use it:

<div>
    {this.state.content}
</div>

On page it just shows the source code directly. How do I get them render as html source code instead of a string?

Upvotes: 0

Views: 653

Answers (1)

kazimt9
kazimt9

Reputation: 563

Actually, you are trying to set raw HTML. Try this.

<div dangerouslySetInnerHTML={{__html: this.state.content}}></div>

Duplicate of this question

Upvotes: 2

Related Questions