Rendering raw html with reactjs

Code Snippets 4 U
Use the html-react-parser npm module to do render your raw html.
Install the module using the following: 
 
npm install html-react-parser --save 

Below is example of conditional rendering (Needed if data is loaded dynamically.  Otherwise the app will crash). It allows parsing only when the content is present in the state.
{
this.state.currentContent &&
parser(this.state.currentContent.toString())
}

If you are using typescript then you have to add a fileName.d.ts file containing following

declare module 'html-react-parser';

Leave a Reply

Your email address will not be published. Required fields are marked *

47 − thirty nine =