React · Junior · Question 38 of 75

What's dangerous about dangerouslySetInnerHTML?

Explanation

React normally escapes strings, so {userInput} is safe. dangerouslySetInnerHTML={{ __html }} inserts markup as is, and <img onerror=...> from user data runs a script. If you really need HTML (e.g. from a CMS), clean it with a sanitizer (DOMPurify).

Further reading

Problem with a question

React · Junior · question 38

Test yourself

Take the React · Junior quiz: questions and options are shuffled, and you get a review of your mistakes at the end.

Take this quiz