Analysis and Solutions for HTML String Rendering Issues in React

Nov 08, 2025 · Programming · 15 views · 7.8

Keywords: React | HTML Rendering | dangerouslySetInnerHTML | HTML Entities | XSS Security

Abstract: This article provides an in-depth exploration of common issues encountered when rendering HTML strings in React applications, with a focus on rendering anomalies caused by HTML entity escaping. By detailing the working principles of dangerouslySetInnerHTML and comparing direct rendering versus escaped rendering, it offers multiple solutions including server-side decoding, client-side decoding function implementation, and third-party library usage. Through concrete code examples, the article helps developers understand the core mechanisms of HTML string rendering and avoid common pitfalls.

Overview of HTML String Rendering Issues

In React development, developers often need to render strings containing HTML tags as actual DOM elements. React provides the dangerouslySetInnerHTML property to achieve this functionality, but in practice, developers may encounter situations where strings are output as-is rather than being parsed as HTML.

Problem Phenomenon Analysis

When using dangerouslySetInnerHTML to render HTML strings, if the strings contain HTML entities (such as &lt; representing <, &gt; representing >), these entities will be displayed directly as text by the browser instead of being parsed as corresponding HTML tags. This occurs because HTML entities are designed to safely display special characters in HTML documents, and when they appear as string content, the browser does not automatically decode them.

Core Issue: HTML Entity Escaping

The root of the problem lies in whether the HTML string has been escaped. Unescaped HTML strings can be correctly rendered directly through dangerouslySetInnerHTML:

const htmlString = '<h1>Hello World</h1>';
<div dangerouslySetInnerHTML={{ __html: htmlString }} />

Whereas escaped HTML strings will be rendered as plain text:

const escapedHtmlString = '&lt;h1&gt;Hello World&lt;/h1&gt;';
<div dangerouslySetInnerHTML={{ __html: escapedHtmlString }} />

Solution Comparison

Server-Side Decoding

The most ideal solution is to handle HTML escaping at the data source. If HTML strings come from a server API, it is recommended to return unescaped raw HTML from the server:

// Server returns unescaped HTML
{
  match: {
    description: '<h1>Hello World</h1>'
  }
}

Client-Side Decoding Function

When it is not possible to modify server-returned data, a HTML decoding function can be implemented on the client side:

class HTMLRenderer extends React.Component {
  decodeHTML(htmlString) {
    const textArea = document.createElement('textarea');
    textArea.innerHTML = htmlString;
    return textArea.value;
  }

  render() {
    const { htmlContent } = this.props;
    const decodedHTML = this.decodeHTML(htmlContent);
    
    return (
      <div dangerouslySetInnerHTML={{ __html: decodedHTML }} />
    );
  }
}

Using DOMParser

Modern browsers support safer HTML parsing using DOMParser:

function decodeHTML(htmlString) {
  const parser = new DOMParser();
  const doc = parser.parseFromString(htmlString, 'text/html');
  return doc.documentElement.textContent;
}

Third-Party Library Solutions

In addition to manual processing, specialized HTML parsing libraries can be used:

react-html-parser

import ReactHtmlParser from 'react-html-parser';

function MyComponent({ htmlString }) {
  return (
    <div>
      {ReactHtmlParser(htmlString)}
    </div>
  );
}

html-react-parser

import parse from 'html-react-parser';

function MyComponent({ htmlString }) {
  return (
    <div>
      {parse(htmlString)}
    </div>
  );
}

Security Considerations

When using dangerouslySetInnerHTML or HTML parsing libraries, cross-site scripting (XSS) attack risks must be considered. Ensure that processed HTML content comes from trusted sources or has undergone appropriate security filtering. For user-generated content, it is recommended to use specialized HTML sanitization libraries (such as DOMPurify) for processing.

Performance Optimization Suggestions

For frequently updated HTML content, consider the following optimization strategies:

Conclusion

The core of HTML string rendering issues lies in correctly identifying and handling HTML entity escaping. By understanding the essence of the problem and selecting appropriate solutions, developers can ensure that HTML content is correctly rendered in React applications while balancing security and performance requirements.

Copyright Notice: All rights in this article are reserved by the operators of DevGex. Reasonable sharing and citation are welcome; any reproduction, excerpting, or re-publication without prior permission is prohibited.