Table of contents
1.
Introduction
2.
1. Use dangerouslySetInnerHTML
2.1.
Example
3.
2. Use a Library
3.1.
Installation
3.2.
Example
4.
3. Using React's Escape Hatches
4.1.
Example
5.
Frequently Asked Questions
5.1.
Why should we avoid dangerouslySetInnerHTML in React?
5.2.
Which library is best for parsing HTML in React?
5.3.
What is the safest way to handle dynamic content in React?
6.
Conclusion
Last Updated: Feb 24, 2025
Medium

HTML React Parser

Author Gaurav Gandhi
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

HTML React Parser is a library that allows developers to convert HTML strings into React components. It is useful when rendering dynamic HTML content from external sources in React applications. This parser helps handle raw HTML while maintaining React’s virtual DOM structure.

HTML React Parser

In this article, you will learn about the features of HTML React Parser, how to use it, and its benefits in React development.

1. Use dangerouslySetInnerHTML

React restricts the direct insertion of HTML into components to prevent Cross-Site Scripting (XSS) attacks. However, if necessary, you can use the dangerouslySetInnerHTML attribute to inject HTML content.

Example

import React from "react";

function HtmlRenderer() {
  const htmlContent = "<h2 style='color:blue;'>Hello, React!</h2>";

  return (
    <div dangerouslySetInnerHTML={{ __html: htmlContent }} />
  );
}
export default HtmlRenderer;

 

Explanation:

  • The __html key holds the raw HTML string.
     
  • React directly injects it into the DOM.
     
  • Security Risk: Be cautious while using this method, as it can lead to XSS attacks if the HTML content comes from an untrusted source.

2. Use a Library

Instead of using dangerouslySetInnerHTML, a safer approach is using libraries like html-react-parser, which automatically converts HTML strings into React elements.

Installation

Run the following command to install html-react-parser:

npm install html-react-parser

Example

import React from "react";
import parse from "html-react-parser";

function HtmlParserComponent() {
  const htmlString = "<p><strong>Bold Text</strong> with <em>italics</em>.</p>";
  return <div>{parse(htmlString)}</div>;
}

export default HtmlParserComponent;

 

Explanation:

  • The parse() function converts the HTML string into JSX elements.
     
  • Security: This method automatically escapes unsafe content, making it a more secure option compared to dangerouslySetInnerHTML.

3. Using React's Escape Hatches

React provides built-in mechanisms like state management and JSX expressions to handle dynamic HTML content without needing direct parsing.

Example

import React, { useState } from "react";
function SafeRenderer() {
  const [text, setText] = useState("Hello, <b>world!</b>");

  return <div>{text}</div>;  // React escapes the HTML automatically
}
export default SafeRenderer;

 

Explanation:

  • The text inside {text} is treated as plain text rather than HTML.
     
  • This method prevents XSS attacks and is safer for handling dynamic content.
     
  • When to use: If you don't need HTML formatting, using escape hatches is the best option.

Frequently Asked Questions

Why should we avoid dangerouslySetInnerHTML in React?

Using dangerouslySetInnerHTML can expose your application to Cross-Site Scripting (XSS) attacks if the HTML content comes from an untrusted source. It should be used with caution and only when necessary.

Which library is best for parsing HTML in React?

The html-react-parser library is a great option as it converts HTML into JSX elements while preventing common security vulnerabilities.

What is the safest way to handle dynamic content in React?

The safest way is to use state variables and JSX expressions, as React automatically escapes HTML content, preventing security risks.

Conclusion

In this article, we discussed HTML React Parser, a library that converts raw HTML strings into React components. It allows developers to dynamically render HTML within React applications while maintaining security and performance. Understanding HTML React Parser helps in handling dynamic content, integrating third-party HTML, and improving flexibility in React development.

Live masterclass