Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

162
Visualizações
Queryselector returning null for an existing element in React

I am not sure why this line of code is returning an error, but I have spent hours on it and I have yet to understand why. I have a div element in the navigation bar with a module class that does not have any changed property within the CSS file, it is just a block. (I have used a module class that does have properties changed within the CSS file but the error has persisted). I did try it with a regular class and the Queryselector found the element, but in this project, I need to use modules. Can anyone please help me understand why this Queryselector returns null when trying to access the div element?

const Nav = () => {
useEffect(() => {
    // console.log(document.querySelector('.testing'))
    window.addEventListener('click', (e) => {
        // console.log(document.querySelector('.testing'))
        if(document.querySelector(`.${styles.testing}`).contains(e.target)){
            // is not able to find the element with the className 'testing'
            // irrelevant code here
        } 
    })
}, [])

return (
  <>
    <div className={`${styles.user_interaction_container} ${styles.list}`}>
        <a href="https://www.google.com/">
            <img src={search} alt="Search" width="16px" height="16px"></img>
        </a>
        <a href="https://www.google.com/">
            <img src={heart} alt="Liked" width="16px" height="16px"></img>
        </a>
        <a href="https://www.google.com/">
            <img src={cart} alt="Cart" width="16px" height="16px"></img>
        </a>
    </div>
    <div className={styles.testing}>
        <img src={menu} alt="Menu" width="20px" height="20px"></img>
    </div>
  </>
)
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.4.2/react.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/15.4.2/react-dom.min.js"></script>
Code doesn't work but I just wanted to show what it looks like. Here is a screenshot of the issue (All of the other Queryselectors work)

enter image description here

What I don't understand is how it is able to find other elements identically called but can't access this specific class??

Any help would greatly be appreciated

about 4 years ago · Juan Pablo Isaza
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda