How to display richtext data in reactjs or how to convert richtext in reactjs?

555 views Asked by At

After fetching richtext editor's data from backend with reactJs the text display with HTML tag inside my div.

fetched data was such as:

const richText = '<p>Hello<strong> World</strong></p><p><strong>Next Line</strong></p>';

Now I want to "Render" the richText in a REACT component as such:

Hello World

Next Line

1

There are 1 answers

0
Ibrahim Khalil On

Step 1:

npm i dompurify

Step 2:

import DOMPurify from 'dompurify'

inside your component where you want to display data:

<div dangerouslySetInnerHTML={{__html:DOMPurify.sanitize(richText)}} > </div>