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

132
Visualizações
Cannot Remove Event Listener

Hello i tried to remove the event click event listener from a button.

In my real problem i have multiple scroolbars synced (whenever one changes the other scrolls to the same position) and i want to remove the onscroll event.

I oversimplified the example, but does not work. Any idea?

import { useRef } from "react";

var btnRef;

function clicked(){
    alert('btn clicked');
    if(btnRef.current){
        console.log('remove click listener ...')
        btnRef.current.removeEventListener('click', clicked);
    }
}

function MyComponent(){

    btnRef = useRef();

    return (
        <button ref={btnRef} onClick={clicked}>click me</button>
    );

}

export default MyComponent;
about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

const [clicked, setClicked] = useState(false);

set clicked to true on button click, add logic to display based on boolean value

about 4 years ago · Juan Pablo Isaza Relatório

0

You need to use state and update it when the button is clicked for the first time, so whenever the button is clicked again, do nothing

function MyComponent(){
  const [clicked, setClicked] = React.useState(false)

  function handleClick(){
      if(clicked){
        return undefiend
      }
      setClicked(true)
      alert('btn clicked');
  }

    return (
        <button disabled={clicked} onClick={handleClick}>click me</button>
    );

}
export default MyComponent;
about 4 years ago · Juan Pablo Isaza Relatório
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