Join our Newsletter — 33% off our NHI Course
Home› FAQ› Cyber Security› What is the difference between rendering text with…
Cyber Security

What is the difference between rendering text with JSX and rendering HTML with dangerouslySetInnerHTML?

← Back to all FAQ
By NHI Mgmt Group Editorial Team Updated October 30, 2026 Domain: Cyber Security

JSX text rendering treats values as strings and escapes special characters, which blocks embedded tags from becoming live DOM elements. dangerouslySetInnerHTML tells React to inject markup directly, so the browser parses it as HTML. Use JSX for ordinary output and reserve dangerous HTML insertion for sanitized content that truly needs formatting.

Why JSX text stays inert while injected HTML becomes live markup

JSX text rendering treats dynamic values as text, so the browser receives escaped characters rather than executable or structural HTML. That means a string such as <em> is displayed literally, not interpreted as a tag. This is the safer default because it preserves the boundary between untrusted content and the DOM.

By contrast, dangerouslySetInnerHTML hands React a string of markup to insert into the page as HTML. The browser then parses that string into DOM nodes, which can change structure, styling, and behaviour. That difference is not just cosmetic, it determines whether input is rendered as data or treated as page content.

For example, JSX text rendering is appropriate for usernames, comments, labels, and API fields where you want plain output. Use HTML injection only when you genuinely need trusted formatting such as embedded links or semantic markup that cannot be expressed cleanly with components. The safe choice is usually the one that keeps content as text until you have explicitly validated the need for markup.

Why the security model is different

The practical security difference is that JSX makes XSS much harder to trigger because tags, event handlers, and script-like payloads remain inert when rendered as text. With injected HTML, the browser evaluates the markup structure, so any unsafe content inside the string can become active DOM. That is why the API is named with a warning: the risk is not theoretical, it is the direct consequence of switching from text output to raw HTML parsing.

This also changes how you reason about trust. If the content originates from users, external systems, CMS fields, or rich-text editors, you must assume it can carry unexpected markup unless it has been sanitised. A safe rendering path should therefore be explicit about provenance, permitted tags, and whether attributes are stripped or rewritten before insertion.

When teams use a rich text pipeline, the hard part is usually not the render call itself but the trust boundary upstream. Sanitisation has to happen before the string reaches the component, and it should be based on a documented allowlist, not ad hoc string replacement. For React applications, this is especially important in places like content previews, support portals, dashboards, and profile fields where a small convenience feature can become a cross-site scripting sink.

See the React and Next.js Security Guide for the broader control patterns around CSP, data leakage, and safe handling of dangerouslySetInnerHTML in React and Next.js applications.

When to use each approach in real code

Use JSX text rendering by default whenever the content is ordinary text. That includes most UI copy, user-generated names, and application data. Choose dangerouslySetInnerHTML only when you have a specific formatting requirement, the source is trusted or sanitised, and you can justify why component-based rendering is not sufficient.

If you do need injected HTML, keep the allowed surface area as small as possible. Limit the tags and attributes that survive sanitisation, and avoid allowing inline event handlers, embedded scripts, or arbitrary URLs. The more expressive the allowed HTML, the more difficult it becomes to reason about the final DOM.

When reviewing code, treat any use of dangerouslySetInnerHTML as a point that deserves a trust review. The relevant questions are: where did the string come from, what sanitisation happened, and can the same effect be achieved with React components instead? If the answer to the last question is yes, component rendering is usually the better choice.

Standards & Framework Alignment

This section maps relevant standards and security frameworks to the operational risks and controls described in this guidance.

OWASP ASVS and CIS Controls v8 set the technical controls, while ISO/IEC 27001:2022 defines the regulatory obligations.

FrameworkControl / ReferenceRelevance
OWASP ASVSV1 — Encoding and SanitizationMarkup injection and output encoding are central to this React rendering choice.
V3 — Web Frontend SecurityThe question is about browser-rendered content and DOM safety in a front-end app.
V15 — Secure Coding and ArchitectureChoosing component rendering over raw HTML is a secure design decision.
Recommendation — Encode output by default and sanitize any user-controlled HTML before insertion. Review front-end rendering paths for injection risks and unsafe DOM insertion. Prefer structured UI components over raw HTML rendering wherever possible.
CIS Controls v8CIS-16 — Application Software SecuritySafe handling of untrusted input in web apps is a core application security concern.
Recommendation — Build secure input handling and output encoding into application code reviews.
ISO/IEC 27001:2022A.8.28 — Secure codingSafe rendering and sanitization are secure coding concerns in application development.
Recommendation — Apply secure coding rules for output encoding and HTML sanitization.

Practitioner Guidance

What to verify: Confirm whether the input is truly trusted, or whether it is merely assumed to be safe because it comes from an internal system. Internal provenance is not the same as sanitised content, especially when the source is user-editable or downstream of another integration.

Decision rule: If the content is plain data, render it as JSX text. If it must contain markup, sanitise it first and then use dangerouslySetInnerHTML only for the minimal portion of the page that actually needs HTML semantics.

Common mistake: Teams often use injected HTML to solve presentation problems that could be handled with components, markdown parsing, or structured fields. That shortcut expands the attack surface and makes later review harder because the safety of the content now depends on hidden assumptions.

Practitioner takeaway: The key distinction is trust, not syntax: JSX treats content as text by default, while dangerouslySetInnerHTML moves that content into the browser’s HTML parser, so the burden shifts to sanitisation and review.

Free weekly newsletter

Subscribe to the NHI & AI Identity Journal

The latest on NHI and Agentic AI security – articles, research, breaches, news and events every week.

Bonus 33% off our NHI Course when you subscribe.

NHIMG Editorial Note
Reviewed and updated by the NHIMG editorial team on October 30, 2026.
NHI Mgmt Group — the #1 independent authority on Non-Human Identity, IAM, and Agentic AI security. nhimg.org