Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to display HTML received in JSON using React

JSON

var obj = {
   content: "<p class="p1">Sample p tag one.</p> <p class="p1">Also 
   another paragraph sample.</p> <p><b>sample text</b></p> <p>More info <a 
   title="" href="https://www.google.com/" 
   target="_blank" rel="noopener"><em>THE WORK AHEAD: MACHINES, SKILLS AND 
   U.S. LEADERSHIP</em></a></p>"
};

How would I render this code in react? It shows all of the html tags.

like image 272
CoolDog Avatar asked Apr 18 '26 06:04

CoolDog


2 Answers

Through dangerouslySetInnetHTML.

const htmlString = '<p>My html</p>';

const innerHtmlObject = {
  __html: htmlString,
};

<div dangerouslySetInnerHTML={innerHtmlObject} />

Note: This is a bad practice that can lead to security vulnerability!

like image 98
Ramon Balthazar Avatar answered Apr 20 '26 21:04

Ramon Balthazar


You can try this

<div
  dangerouslySetInnerHTML={{
    __html: this.state.obj.content
  }}
/>
like image 24
Adnan Avatar answered Apr 20 '26 21:04

Adnan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!