-
Notifications
You must be signed in to change notification settings - Fork 50.4k
Closed
Description
I think many people agree that the dangerouslySetInnerHTML={{ __html: ... }} api is gross even though there is great reasoning behind it.
I have a few issues with it beyond it's verbosity that I think could be added as static method on the react class.
My Issues with dangerouslySetInnerHTML:
- Verbose and not always dangerous.
- Can't mix safe and unsafe html.
- Can't mix html with react elements.
- Have to manually concat html strings.
A better solution would be to provide a way to mark html as "safe".
var React = require("react");
// Use #markSafe method to bypass reacts automatic html escaping.
var myReactEl = <div>{ React.markSafe("<br/>") }</div>;This is still explicitly telling react that we trust the html but solves all of the problems above.
// Mixing safe and unsafe html.
(
<div>
{ React.markSafe("<br/>") }
{ "<br/>" }
</div>
);
// Mixing html with react elements.
(
<div>
{ React.markSafe("<br/>") }
<span>Hello!</span>
</div>
);
// Multiple innerHTML sets.
(
<div>
{ React.markSafe("<br/>") }
{ React.markSafe("<hr/>") }
</div>
);I think this api would be much friendlier than the current html api and probably wouldn't even require a major version bump.
Thoughts?
nickserv