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.
useRefcouldn't help 🤔 just curious ...querySelector😉document.body. To check if I clicked outside the select I was checking ifevent.targethas a parent which is opened select. I usedElement.closestwhich accepts string selector..closestmethod works the same way as thequerySelector, so I simplified the question.