Skip to content

Feature Request: innerHTML alternative #6253

@DylanPiercey

Description

@DylanPiercey

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:

  1. Verbose and not always dangerous.
  2. Can't mix safe and unsafe html.
  3. Can't mix html with react elements.
  4. 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?

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions