Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RFC: RawHTML Component #129

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
82 changes: 82 additions & 0 deletions text/0000-dangerous-html-without-wrapper.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
- Start Date: 2019-11-06
- RFC PR: (leave this empty)
- React Issue: (leave this empty)

# Summary

ReactDOM.RawHTML is a new component that lets you dangerously set innerHtml without a wrapper element.

# Basic example

```jsx
import * as ReactDOM from 'react-dom';

function Layout(props){
return (
<html>
<head>
<ReactDOM.RawHTML dangerouslySetInnerHTML={{ __html: props.headerHTML }} />
</head>
<body>
hello
</body>
</html>
)
}

ReactDOM.renderToString(
<Layout headerHTML={`<script src="/app.js"></script> <link href="/styles.css" />`} />
)
```

```html
<html><head><script src="/app.js"></script> <link href="/styles.css" /></head><body>hello</body></html>

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for this RFC, but I'm not sure if this case is clear enough 🤔 Just to make sure, do we really need this feature? No workaround here?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have the same problem and I just want to inject a JS snippet.

```

Uses the same `dangerouslySetInnerHTML` keyword as DOM elements, but when rendered does not include any wrapping DOM element.

# Motivation

Today in React, one must wrap dangerous html in an element ```<div dangerouslySetInnerHtml={{__html: `<span>ok</span>`}} />```.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In order to avoid adding new component, maybe would be better to extend Fragment?

Example:

<Fragment dangerouslySetInnerHtml={{__html: '...'}} />

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See this comment on the issue linked directly above. It seems this RFC was created because the specific need of raw HTML is one created at the ReactDOM level, while Fragment comes from React proper.

This results in an unwanted wrapper element, which in the head tag is illegal.


# Detailed design

`RawHTML` is a new component that lives in ReactDOM.

`RawHTML` only accepts a prop of `dangerouslySetInnerHTML`.

The prop type of `dangerouslySetInnerHTML` is an object with key `__html` and value `string`.

When rendered the output is the content of `__html`.


# Drawbacks

- An additional way to set dangerous html.
- Discovering the API might be difficult, folks might look to dangerouslySetInnerHTML on a Fragment first.


# Alternatives

- https://github.com/remarkablemark/html-react-parser
Copy link

@milesj milesj Nov 6, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Or safely render HTML (dangerously is bad). https://github.com/milesj/interweave



# Adoption strategy

- Non-breaking
- Could coordinate with html-react-parser to have them help distribute the update


# How we teach this

- There have been issues filed for renaming dangerouslySetInnerHTML, but I think consistency would be prefered.
- Adding to the docs near https://reactjs.org/docs/dom-elements.html#dangerouslysetinnerhtml


# Unresolved questions

- Which package does this component belong in?
- Are there repurcussions for comparisons across renders?