1 April 2020

Rendering raw html data in react js

  • Create Components folder under src folder
  • Add RenderingRawHtml.js file and add below Code
import React, { Component } from "react";
import Parser from "html-react-parser";

class RenderingRawHtml extends Component {
constructor(props) {
super(props);
this.state = { htmlData: "<div><h2><u>HTML Data</u></h2></div>" };
}
render() {
return (
<>
<h3>Rendering raw html data in reactjs Example</h3>
<div>{Parser(this.state.htmlData)}</div>
</>
);
}
}
export default RenderingRawHtml;
  • Open App.js file and update code as below\
import React from "react";
import ReactDOM from "react-dom";
import RenderingRawHtml from "./components/RenderingRawHtml";

function App() {
return (
<div className="App">
<RenderingRawHtml />
</div>
);
}

const rootElement = document.getElementById("root");
ReactDOM.render(<App />, rootElement);

Output:







No comments:

Post a Comment