Categories
React Answers

How to avoid HTML escaping of text children when calling React.createElement?

Spread the love

To avoid HTML escaping of text children when calling React.createElement, we use the dangerouslySetInnerHTML prop.

For instance, we write

const Component = () => (
  <span dangerouslySetInnerHTML={{ __html: "&gt;&lt;" }} />
);

ReactDOM.render(<Component />, document.getElementById("container"));

to create the Component component that renders the "&gt;&lt;" as raw HTML by setting it as the value of __html in the object that we set as the value of the dangerouslySetInnerHTML prop.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *