I am trying to get the element by id. (It's an example code to reproduce the error)
function MyComponent(){
const myId = useId();
useEffect(() => {
const myComponentDOMElement = document.querySelector(`#${myId}`); // error here
}
)
return <div id={ myId }> text </div>
}
This code gives an error:
Uncaught DOMException: Failed to execute 'querySelector' on 'Document': '#:Rt6m:' is not a valid selector.
useRef couldn't help me in my case. How can I use the ID to get the element.
useRef
couldn't help π€ just curious ... β WoolfolkquerySelector
π β Woolfolkdocument.body
. To check if I clicked outside the select I was checking ifevent.target
has a parent which is opened select. I usedElement.closest
which accepts string selector..closest
method works the same way as thequerySelector
, so I simplified the question. β Lajoie