Skip to content
Tutorials Center
My learning

Topics

React Reference ยท Props and Attributes

React dangerouslySetInnerHTML prop

Reference for dangerouslySetInnerHTML in React: insert raw HTML from a string, why it is dangerous, how to escape or sanitize content, and its rules and errors.

dangerouslySetInnerHTML replaces an element's content with an HTML string, bypassing React's normal escaping. It is React's version of the DOM innerHTML property, and the long name is deliberate: if any part of the string comes from a user, it can inject scripts and attributes into your page. That attack is called cross-site scripting (XSS).

Normally you never need it. in JSX is escaped automatically, and rich content is better expressed as components. Use this prop for HTML that you generated yourself or sanitized, such as the output of a Markdown renderer, a CMS field that is cleaned on the server, or an SVG string from a trusted icon set.

JSX
<div dangerouslySetInnerHTML={{ __html: '<strong>Trusted</strong> markup' }} />

Try it

Escaped text versus raw HTML
Escape first, then allow a tiny safe subset

Syntax

JSX
<element dangerouslySetInnerHTML={{ __html: htmlString }} />
Name Description
dangerouslySetInnerHTML An object with a single __html key holding a string of HTML.
__html The markup. It is assigned to the element's innerHTML when it changes.

Returns: the element, with its children created by the browser's HTML parser rather than by React.

Errors: React throws Can only set one of children or props.dangerouslySetInnerHTML if the element also has children, and props.dangerouslySetInnerHTML must be in the form {__html: ...} if you pass a plain string.

Usage notes

  • Never insert unsanitized user input. <img src=x onerror="..."> runs script even though <script> tags do not run when assigned through innerHTML. Escape the text, or pass it through a maintained sanitizer such as DOMPurify (not available in the TryIt sandbox) with a strict allow-list.
  • Sanitize on the server too. Stored content that is cleaned only in the browser can still be abused by other clients. Prefer cleaning once, before saving, and again with an allow-list when rendering.
  • Script tags do not execute when inserted this way, but event-handler attributes, javascript: URLs and CSS can still cause harm.
  • React does not look inside. The inner nodes are invisible to React: no keys, no events from onClick props, no state. Attach behavior to the wrapper element, for example with event delegation or a ref.
  • Updates replace everything. When __html changes, React rewrites the whole innerHTML (it compares the strings, not the DOM). A string that stays the same does not touch the DOM, so user selection or a scroll position inside the content survives re-renders.
  • Do not combine with children. The element must be empty in JSX; wrap other content in a sibling element.
  • Hydration. The server must render the same string as the client, or React logs a mismatch.
  • Alternatives. For structured content, map data to components. For Markdown, use a renderer that outputs React elements. For SVG icons, make them components. See SVG elements.
  • A Content Security Policy that forbids inline scripts and unknown sources is a useful second line of defense, not a replacement for escaping.
  • The odd __html wrapper exists so that unsafe usage is easy to spot in code review and search.