-
Notifications
You must be signed in to change notification settings - Fork 45.9k
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
React.createComment() would be nice #2810
Comments
The API would be |
Hmm, if a comment is a DOM element, why would it not be a part of |
Edit: Also note that technically "comment" is not a DOM element. It's a DOM node. (Unless you're talking about |
Ah, yes, agreed. |
Would comments be diffed and part of the reconciliation? Are they stripped in production? Btw you can make a custom Comment component that renders to null in production (but visible in devtools in development). Not sure about the benefits though. |
@sebmarkbage Hmm, perhaps a comment can be considered a special case and that's why it makes sense, but I'm not sure it does, a comment node is not universally useful and only really useful for the DOM frontend? Other than that it should behave like regular ReactElement ... so does it really make sense that it would have its own EDIT: I guess it could make sense if we consider that comments could be a helpful debugging tool for any front-end and that it would map to no-op for those that can't make use of it. Hmm... |
The browser XML/HTML DOM has 9 interesting kind of nodes in the document:
It's useful to be able to render all of these if you use React to server-side render arbitrary XML documents. Of these, we already have support for five of them:
We could add support for the final four as well to get complete support:
The
React.renderToString(
React.createDocument({
children: [
React.createDocumentType('html', '', ''),
React.createProcessingInstruction('access-control', 'allow="*.example.com"'),
React.createComment('Hello this is a secret message for view-source'),
React.createElement('html', ...)
]
})
) |
@sebmarkbage Hmm, I'm a bit confused, I thought the idea was for Just to illustrate my thoughts, it would make sense to me if it was exposed as PS. The React signature for fragment shouldn't be |
@syranide You're right. However, currently the It's an interesting exercise though. Would it make sense to have the ability to define a "comment" in other environments too? The others are probably very unique to DOM. PS. We might need an additional representation for a keyed fragment but there is some discussion going on in that space. Currently it's just |
@sebmarkbage For the future I guess... :) If we expand the context a bit, it seems to me it's a matrix of:
A. Exists in React only (avoids instancing overhead) 1A, 1B, 1C makes sense to me at some level, a good developer tool at A/B/C should make it largely pointless, but it seems there's some legitimacy to it still I think. 2A makes no sense to me I think, that's what props are for? 3A, 3B is currently used quite a bit I believe (no-op composite components) That's what I could make sense of right now, if it makes any sense... it seems to me there could be some value in comments as a "universal API", however slim it might be. |
Anything happening with this? |
Not currently. |
It seems like the use case for this is very niche, and the effort to make this happen is more than trivial. I’ll close as we’re unlikely to work on this. |
It's been 6 years, is there a workaround, or even a chance this could get added now? |
Would be nice to be able to insert html comments into the DOM. I ran into wanting to do this. Hope this gets some traction. Thank you! |
It'd just have to use
document.createComment()
.It'd come in handy when working in the Inspector and when compiling to React from other template systems.
The text was updated successfully, but these errors were encountered: