I am trying to display HTML content on React front side.
<p>Hello/<p>
Aadafsf
<h1>H1 in hello</h1>
I write this from CKEditor from Admin side
<p>Hello/<p>
Aadafsf
<h1>H1 in hello</h1>
<p>Hello/<p>
Aadafsf
<h1>H1 in hello</h1>
dompurifyimport {Parser} from "html-to-react";
import DOMPurify from "dompurify";
{Parser().parse(DOMPurify.sanitize(HTMLCONTENT))}

I tried this but I'm not getting the expected output.
dangerouslySetInnerHTMLI have the below HTML content:
<h2>This is Heading</h2><p><p></p></p><p>This is para.</p><p> </p><p><strong>This is Bold</strong></p><p> </p><ol><li><strong>dwsfdf</strong></li><li><strong>fdsfdf</strong></li></ol><p> </p><p> </p><p> </p><figure class="media"><oembed url="https://youtu.be/rokGy0huYEA"></oembed></figure>
I used it like:
<div dangerouslySetInnerHTML={{__html: HTMLCONTENT}} />;
Actually, your mistake is not on the serialization, it is because there is a typo in your HTML.
The <p> tag is "closed" by a /<p> tag and not by a </p> !
So you need to update your HTML:
- <p>Hello/<p>
+ <p>Hello</p>
Aadafsf
<h1>H1 in hello</h1>
Also, in order to display raw HTML in your component. React offer a function for that dangerouslySetInnerHTML.
But, take real care as this can open your site to attacks known as cross-site scripting (XSS).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With