-
Notifications
You must be signed in to change notification settings - Fork 47.2k
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
Feature Request: innerHTML alternative #6253
Comments
At first glance, I like this API much better than our current API. The function name would need a bit of bikeshedding. At the very least, it would need to live on cc @syranide @sebmarkbage Thoughts on such an API? |
@jimfb I agree the function name could use some work. I think it should still be simpler than "dangerouslySetInnerHTML" though. I think something like |
@jimfb I think it's the "correct API", it was my intention to propose this some time ago so I'm 👍. But it seems technically prohibitive to do, by using But that doesn't mean we can't change the API to be EDIT: However, given our new render this might actually be realistic? |
Is this proposal just a sugar wrapper around React.createElement, or does it do something differently? Bikeshedding: the name implies it makes the html safe, which it doesn't, but that can be done. |
@syranide could do something like wrapping the user html with two comment tags? I think the "allowing only one child" thing although still pretty looses all of the advantages I mentioned. @brigand its different in that it allows user html to exist with react html within the same parent container. It's not something that is acheivable in userland since you must return a single react element. |
FWIW, in Django it's called |
@sthzg I like @syranide Although I initially agreed with |
@DylanPiercey thanks for the clarification. I like markSafe or markAsSafeHTML. |
Not really my expertise, but it seems logical to me that the API would be I.e. to put it differently, IMHO |
@syranide I understand restricting the api to be on Having said that i'd still much rather have even the fragment api than the current |
It's just a naming proposal, it conceptually replaces dangerouslySetInnerHTML, not the "safe" html-object. There may be ReactDOM.createComment in the future which may map to a special syntax in JSX, or it may need to be called "manually", don't know. It's also possible it will simply be mapped to a regular tag name, say "comment", "!comment" or w/e. It seems to me that HTML among children is the same problem and should thus be solved the same way. If you want to preserve the "safeness", then that goes on-top of that API, and even though it may look gross I would still be OK with it as it's conceptually about casting a string to a HTML-object/string type which is then understood by createHTMLFragment which would return an element with an imaginary internal type of ReactDOMHTMLFragment. |
The api here does seem nicer, naming-wise though it would be a mistake to move away from the "WATCH OUT!" nature of the current api's name. There is a lot of value in signally to users that an action potentially has risks. I like the approach Rust took with its Similarly for this, you are intentionally opting out of the safety provided by the React component model. The notion of danger in the current API is not signally that you should put dangerous HTML there, only that you now CAN. |
@jquense I am wondering though if in a long term it wouldn't be nicer to keep such verbose warnings behind a warning that pops up in the developer tools as long as the app isn't built for production or the user opts out by some means (e.g. some option or flag, or an explicit import like Scala does it if you want to use experimental or advanced compiler features). One advantage of such an approach might be that the warning messages could be better articulated and more expressive than the name of a function/method might ever get. |
I just talked with @sebmarkbage about this, here are the results. Overall, Sebastian indicated that the new API was probably ok, but a change should probably be low priority. His main concern was performance in the case where React nodes are siblings of the foreign markup (updating the foreign markup might require iterating over the nodes to delete them individually because you need to figure out which nodes are foreign and where the react siblings are located). However, I now wonder if React should even have a dangerousHtml feature. Setting inner html is a very non-reacty pattern; it's clearly an escape hatch. An alternative is to attach a ref to a DOM node, and set the innerHTML of the DOM node. There is no fundamental reason this can't be handled entirely within userland. |
@jimfb Thanks for the update. Would the second paragraph also hold true for server side rendering? If I understand the approach it would be something like |
@jimfb With the caveat that it must be wrapped in a container node, which aren't "transparent" (there is no node you can put that always work). If there one could insert document fragments into the DOM and keep them there I would agree, but you can't.
AFAIK that was a large part of the initial sell, that you can leave React and it feels perfectly natural (many other frameworks can't). Also, I wouldn't label it an escape hatch, I would it interoperability.
IMHO, implementing this in-terms of #6263 (being able to have DOM nodes as children) in a way makes the most sense to me, then it would be predictable. It could even be an entirely user-land feature if we didn't care about SSR (sigh). |
Ah, thanks @sthzg, we had neglected to consider SSR. You're right, we need some sort of API! I'm back in favor of this new API proposal. |
Please keep SSR in mind 👍 |
+1 to this proposal. A place where this is incredibly useful is when you're getting some cooked HTML from the server and displaying inside a React component. Right now, I have no clean way of treating links inside such HTML to be caught by react-router, and I wish I could just dangerously-set |
Now that we are using comment-nodes to delimit text-nodes, perhaps we could use the same principle but simply render raw HTML instead of text. If we replace everything between the comment nodes when the HTML is updated then it should be safe (SSR is always dangerous with malformed HTML, but that's already an issue). |
#7361 proof-of-concept implementation if anyone is curious |
If you’re interested in this please create an RFC so we can review a specific proposal: https://github.com/reactjs/rfcs I’ll close the issue as it’s unlikely to progress without an RFC. |
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
:A better solution would be to provide a way to mark html as "safe".
This is still explicitly telling react that we trust the html but solves all of the problems above.
I think this api would be much friendlier than the current html api and probably wouldn't even require a major version bump.
Thoughts?
The text was updated successfully, but these errors were encountered: