Coder Social home page Coder Social logo

Comments (6)

Zywz95 avatar Zywz95 commented on April 25, 2024 2

OK.looks it's been fixed.thanks.

from react.

Zywz95 avatar Zywz95 commented on April 25, 2024 1

@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.

from react.

pwbriggs avatar pwbriggs commented on April 25, 2024

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! ❀️

from react.

unknowablesymbol avatar unknowablesymbol commented on April 25, 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!

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

from react.

pwbriggs avatar pwbriggs commented on April 25, 2024

@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>;
}

from react.

pwbriggs avatar pwbriggs commented on April 25, 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.

from react.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.