Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Whats the non-jsx equivalent of dangerouslySetInnerHTML?

Tags:

syntax

reactjs

I'm working through the reactjs tutorial but am trying to avoid jsx and use the virtual DOM manually. How do I do the equivalent of

<span dangerouslySetInnerHTML={{__html: rawMarkup}} />
like image 816
George Mauer Avatar asked Jun 11 '14 03:06

George Mauer


People also ask

What can I use instead of dangerouslySetInnerHTML in React?

However, if the application or site accepts multiple users' data input, you should be concerned. However, apart from that, there is one alternative to using dangerouslySetInnerHTML, simply setting innerHTML of a React HTML element using vanilla JS instead.

Is it OK to use dangerouslySetInnerHTML?

As the name of the property suggests, it can be dangerous to use because it makes your code vulnerable to cross-site scripting (XSS) attacks. This becomes an issue especially if you are fetching data from a third-party source or rendering content submitted by users.

Why is it called dangerouslySetInnerHTML?

The name dangerouslySetInnerHTML is intentionally chosen to remind you that it's dangerous and may cause XSS vulnerabilities so that you make sure your HTML is sanitized before you insert it in your react app.

Can we use innerHTML in React?

This rule applies when innerHTML prop for a React DOM element is used. innerHTML prop is risky because it is easy to expose your users to a cross-site scripting (XSS) attack.


1 Answers

Found the answer plugging around the source code:

React.DOM.div({ dangerouslySetInnerHTML: {
    __html: markdown.makeHtml(this.props.children.toString())
} });

or in coffeescript:

 R.div dangerouslySetInnerHTML: __html: markdown.makeHtml @props.children.toString()
like image 113
George Mauer Avatar answered Oct 08 '22 20:10

George Mauer