Author avatar

Gaurav Singhal

Returning HTML Elements from JSON in React

Gaurav Singhal

  • Sep 24, 2020
  • 4 Min read
  • 113 Views
  • Sep 24, 2020
  • 4 Min read
  • 113 Views
Web Development
Front End Web Development
Client-side Frameworks
React

Introduction

Many modern web apps have the ability to format input text, such as by adding indentation, changing font size or font style, making text bold or italic, and so on. These apps retain the formatting of that text by storing the content along with its markup in the database. The server then returns the entire markup as a string to the frontend, which returns the HTML elements from that JSON to show them on the page.

This guide will walk you through the process of rendering HTML elements directly from a JSON response on the page.

Using the dangerouslySetInnerHTML prop

In React, you can set the inner HTML of a container using the dangerouslySetInnerHTML prop. So if you are using the following line of JavaScript to set the inner HTML of a <div> container ...

1
2
3
const htmlDiv=document.querySelector('div');
const htmlPart='<p>Welcome to this <strong>page</strong></p>'
htmlDiv.innerHTML=htmlPart;
javascript

... you can do the same by passing the dangerouslySetInnerHTML prop to your container and passing an object to it with the key __html and value of the HTML you want to set that container's inner HTML to. You can do the above in React as:

1
2
3
4
5
6
7
8
import React from 'react';

const RenderHTML=()=>{
	const htmlPart='<p>Welcome to this <strong>page</strong></p>'
    return(
      <div dangerouslySetInnerHTML={ {__html: htmlPart} } />
    )
}
jsx

Let's look at the practical implementation with a detailed example.

Implementation in React

In this example, you'll create a dummy JSON response that contains some HTML elements as a string and returns them to the DOM. The use case demonstrated in this example pertains to a job portal where a recruiter posted a job with a formatted job description. On showing this job to users, you must show the job description with the same formatting as it was posted.

Creating Dummy JSON

You can mock any API by storing its JSON data inside a JavaScript file and exporting it as an object. Create an empty JavaScript file called data.js with the following code:

1
2
3
export default {
    "Job Description":  "<h4> Supply Chain Manager @<strong><i>CargoChainzz Pvt Limited </i></strong></h4><br/><p>This job requires at least 3 years of experience</p>"
}
javascript

Now you can import this object anywhere.

Using the dangerouslySetInnerHTML prop

Import the data object inside the component in which you want to render it.

1
import data from './data';
javascript

You can either use this data object directly or set the required property of this object to your component's state inside a lifecycle hook. For brevity purposes, let's use the object directly.

Inside an empty <div>, pass the dangerouslySetInnterHTML prop with an object of key __html and value as data["Job Description"], as Job Description property of data contains your HTML elements as a string .

1
<div dangerouslySetInnerHTML={{__html:data["Job Description"]}}></div>
jsx

Now your HTML elements from JSON are returned from the component in JSON to the DOM.

Conclusion

The dangerouslySetInnterHTML prop is convenient to use, as demonstrated in this guide, and has plenty of use cases. However, according to React's official documentation, setting HTML from code is risky because it’s easy to inadvertently expose your users to a cross-site scripting attack, which is why it is named so. Hence, you should be cautious and aware of the vulnerabilities you are exposing your app to.

0