Closed
Description
React version:18.1.0
Steps To Reproduce
1.import {useId} from 'react' <div id={useId()}></div>
2.document.querySelector('#:r1:')
Link to code example:https://codesandbox.io/p/sandbox/hardcore-williams-yzyvqv
The current behavior
Failed to execute 'querySelector' on 'Document': '#:r1:' is not a valid selector.
Metadata
Metadata
Assignees
Type
Projects
Milestone
Relationships
Development
No branches or pull requests
Activity
pwbriggs commentedon Feb 21, 2024
Hey there @Zywz95! 👋
I'm not sure I understand the problem you're experiencing. Can you tell me
document.querySelector
directly? Is it possible to write the logic within the component itself (perhaps within an effect)?I can't seem to access your Code Sandbox repro, maybe you need to change its permissions to publicly viewable?
Thanks! ❤️
unknowablesymbol commentedon Feb 22, 2024
Hi @Zywz95, I think you're running into the same issue as #27553
It's because the reserved character
:
has special meaning in CSS for pseudo-classes and needs to be escaped.Thankfully, there's a built-in method for this:
CSS.escape(selector: string)
.Something like this should work for your case!
pwbriggs commentedon Feb 22, 2024
@bonelessgalaxy: true.
That said, @Zywz95, be careful about hardcoding the value
:r1:
—there's no guarantee thatuseId
always return that. I'd do something more like this:Zywz95 commentedon Feb 22, 2024
@pwbriggs @bonelessgalaxy
Thank you for your answer. I have now fixed my CodeSandbox link to be publicly accessible, which indeed can solve my problem. What I am expecting is that the ID generated directly by the useId method should not need escaping and can be used directly. Although this approach does solve my problem, I still believe there are compatibility issues because using the character ':' in a DOM id is not a common practice. Regardless, thank you for your response.
pwbriggs commentedon Feb 22, 2024
Yeah, I agree the
:
makes everything more complicated. There's an ongoing discussion about this at #26839, so I think we can close this issue.Zywz95 commentedon Feb 22, 2024
OK.looks it's been fixed.thanks.
feat(vue3): ✨ use Teleport