Command Palette

Search for a command to run...

Hectal
Back to the lesson: Topic 21.1 — How React Protects You from XSS
React · Example 2 of 3

A ref with innerHTML skips React's protection

The first line, this would have run, is the proof: the browser parsed the string, created an img, failed to load x and ran the onerror attribute as code. A real attacker would put something harmful there. The textContent version shows the same string as plain text.

XSS happens when text from someone else is treated as code in your page. JSX escapes every string you put in text or attributes, so it always shows as text, but refs with innerHTML, dangerouslySetInnerHTML, unsafe URLs, spread props and server-rendered state can still let it in.

Change the code and press Run (Ctrl+Enter). Predict what will happen first, then break it on purpose and read what changes. Your edits are saved and match the lesson page.

Ready for more? This phase has 5 hands-on exercises with automatic checks.

Practice questions

Write the code in the editor, run it, then open the model answer to compare.

01

A component shows a search term from the URL: div.innerHTML = 'Results for ' + q. Rewrite it safely in React.

02

Write a small helper serializeState(obj) that is safe to put inside <script type="application/json">, and show how the client reads it back.

03

Write a component that shows a user's name in bold inside a sentence ("Welcome back, Asha!") without using any HTML strings.

Explain it without notes

01

What is XSS, and what can an attacker do once their script runs in your page?

02

Exactly why is <p>{userText}</p> safe in React, both in the browser and in server rendering?

03

List the ways XSS can still get into a React app.

04

Why is putting JSON.stringify(data) inside a <script> tag dangerous, and how do you fix it?

A ref with innerHTML skips React's protection

Preview

Press Run to see the page.

Console

Nothing logged yet.

Expected console output

this would have run
img inside .safe: 0
img inside .unsafe: 1