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

103
Visualizações
How to prevent event listener from being registered twice in react

I am working on a site. The site has two languages. English and arabic. It also has footer and the footer is turned into clickable tabs when it comes to smaller screens. The click functionality works as expected on one version of language. For example, it works good on english version. When i switch to arabic version, the site reloads and the tab onClick event seems to be triggered twice. It seems that the eventHandler is being registered twice when I switch the language.

How can I prevent eventListener from being registered twice?

Below is the code.

import { shape, string } from 'prop-types';
import React, { useEffect, useRef } from 'react';

import { useWindowSize } from '@magento/peregrine/lib/hooks/useWindowSize';
import { useStyle } from '@magento/venia-ui/lib/classify';
import CmsBlock from '@magento/venia-ui/lib/components/CmsBlock';

import defaultClasses from './footer.module.css';

const Footer = ({ classes }) => {
    const isMobile = useWindowSize().innerWidth < 1024;

    const initialized = useRef(false);

    useEffect(() => {
        if (!initialized.current && isMobile) {
            setTimeout(() => {
                initialized.current = true;
                const buttonElements = document.querySelectorAll('.col-links');

                for (let i = 0; i < buttonElements.length; i++) {
                    if (isMobile) {
                        buttonElements[i].addEventListener('click', () => {
                            console.log("clicked")  // gets printed twice on language switch
                            buttonElements[i].classList.toggle('active-tab');
                        });
                    }

                    if (initialized.current && !isMobile) {
                        buttonElements[i].classList.remove('active-tab');
                    }
                }
            }, 500);
        }
    }, [isMobile]);

    const rootClasses = useStyle(classes, defaultClasses);

    return (
        <footer id="footer" className={rootClasses.root}>
            <div className="footerWrapper">
                <CmsBlock identifiers="footer" />
            </div>
        </footer>
    );
};

export default Footer;

Footer.propTypes = {
    classes: shape({
        root: string
    })
};
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