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

119
Visualizações
Responsive Hamburger Menu is not working on Mobile when Clicked

I have created my Responsive Hamburger menu in React. It works fine on Desktop however the hamburger doesn't drop when clicked, it shows this error when clicked ::

TypeError: Cannot read properties of null (reading 'classList')
hamClick
src/hamburger.js:24
  

    21 | 
      22 | const hamClick = (e) => {
      23 |     e.preventDefault();
    > 24 |     if(header.classList.contains('open')){//close hamburger
         | ^  25 |         body.classList.remove('noscroll');
      26 |         header.classList.remove('open');
      27 |         fadeElems.forEach(function(element){

THIS is my code for the Hamburger functionality;;

import React from 'react';

import "./scss/animations.scss";
import './scss/header.scss';
import './scss/mixins.scss';




const Hamburger = () => {

    const btnHamburger = document.querySelector('#btnnHamburger');

    const body = document.querySelector('body');

    const header = document.querySelector('.header');

    const overlay = document.querySelector('.overlay');

    const fadeElems = document.querySelectorAll('.has-fade');
    
    const hamClick = (e) => {
        e.preventDefault();
        if(header.classList.contains('open')){//close hamburger
            body.classList.remove('noscroll');
            header.classList.remove('open');
            fadeElems.forEach(function(element){
                element.classList.remove('fade-in');
                element.classList.add('fade-out');
            });    
        }
        else{//open hamburger
            body.classList.add('noscroll');
            header.classList.add('open');
            fadeElems.forEach(function(element){
                element.classList.add('fade-in');
                element.classList.remove('fade-out');
            });   
        }
    }


    return(
        <a id="btnnHamburger" className="header__toggle hide-for-desktop" href="#" onClick={hamClick}>
            <span></span>
            <span></span>
            <span></span>
        </a>
    )
}

export default Hamburger;

Please can you help me understand what I need to do correctly

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