-
Notifications
You must be signed in to change notification settings - Fork 561
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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> | ||
``` | ||
|
||
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>`}} />```. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 Example:
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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? | ||
|
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.