Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

133
Views
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 answers
Answer question

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!