Skip to content

Bug: Use useId function to generate dom id,it can't use document.querySelector function. #28404

Closed
@Zywz95

Description

@Zywz95

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.

Activity

pwbriggs

pwbriggs commented on Feb 21, 2024

@pwbriggs

Hey there @Zywz95! 👋

I'm not sure I understand the problem you're experiencing. Can you tell me

  1. What do you expect to happen?
  2. Why are you trying to use 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

unknowablesymbol commented on Feb 22, 2024

@unknowablesymbol

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!

document.querySelector(`#${CSS.escape(":r1:")}`)
pwbriggs

pwbriggs commented on Feb 22, 2024

@pwbriggs

@bonelessgalaxy: true.

That said, @Zywz95, be careful about hardcoding the value :r1:—there's no guarantee that useId always return that. I'd do something more like this:

import { useEffect, useId } from "react";

function MyComponent() {
  const id = useId(); // 👈 Put it in a variable

  useEffect(() => {
    const element = document.querySelector(`#${CSS.escape(id)}`);
    // Do something with `element`
  }, [id]);

  return <div id={id}>Hello world!</div>;
}
Zywz95

Zywz95 commented on Feb 22, 2024

@Zywz95
Author

@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

pwbriggs commented on Feb 22, 2024

@pwbriggs

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

Zywz95 commented on Feb 22, 2024

@Zywz95
Author

OK.looks it's been fixed.thanks.

added a commit that references this issue on Apr 23, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Status: UnconfirmedA potential issue that we haven't yet confirmed as a bug

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @Zywz95@unknowablesymbol@pwbriggs

        Issue actions

          Bug: Use useId function to generate dom id,it can't use document.querySelector function. · Issue #28404 · facebook/react