Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Displaying HTML Body on a page using ReactJS

Tags:

html

reactjs

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.

like image 438
Rayes Avatar asked Jun 03 '26 03:06

Rayes


1 Answers

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 }} />
like image 140
Rayes Avatar answered Jun 05 '26 01:06

Rayes