3

Say I have the following code in my database (user input):

<html>
  <title>Test</title>
  <body>testing website</body>
</html>

And I fetched it correctly from my database using ReactJS. How can I display this in say: 'localhost:3000/play'? I don't want it to be rendered as raw data just like the code but I want it to actually render the html body as a website. (Title set to Test, and displays a small text: testing website). How can I do that in ReactJS? I already have /play configured and I just want to know how to display it there in the index.js file. I tried something like <div dangerouslySetInnerHTML={template} /> but it didn't work.

3
  • make sure your template is a json in form of {__html: 'First &middot; Second'}. the __html attribute is important. Commented Jul 9, 2018 at 6:25
  • What I am doing is, const innerHtml = { __html: snapshot.val() } return(<div dangerouslySetInnerHTML={innerHtml} />) and when I log innerHtml I get this {__html: "<html> <title>Test</title> <body>testing website</body> </html>"} Commented Jul 9, 2018 at 6:38
  • jaketrent.com/post/update-body-class-react Commented Jul 9, 2018 at 7:06

2 Answers 2

1

So, I fixed it with the following: In a separate function:

db.getHTMLBody(key).then(snapshot => { this.setState({ body: snapshot.val() }) })

then in the render:

 <div dangerouslySetInnerHTML={{ __html: this.state.body }} />
Sign up to request clarification or add additional context in comments.

Comments

0

Please try :

const innerHtml = { __html: escape(snapshot.val()) } 
return(<div dangerouslySetInnerHTML={innerHtml} />)

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.