Home > OS >  Function firing twice on click in React component
Function firing twice on click in React component

Time:06-16

TL/DR: My simple toggle function fires twice when button is clicked.

I'm using useEffect in a React (w/ Next.js) component so that I can target the :root <html> tag for which I need the class to be toggled. The code is the following:

useEffect(() => {
   const toggleMode = () => {
      const root = document.documentElement;
          
      root.classList.toggle("dark");
      console.log("click");
   };

const toggleBtn = document.querySelector("#toggle-btn");

toggleBtn.addEventListener("click", toggleMode);

I have the necessary imports, the code is placed inside the main component function before the return, and there's no errors in the console at all.

The only issue is that the function is fired twice every time the button is clicked and I cannot find any reason why or solutions online.

Would really appreciate your help and please let me know if I'm missing any information.

Cheers!

CodePudding user response:

Your problem is coming from registering the event listener in a non-react way.

By registering the listener via

const toggleBtn = document.querySelector("#toggle-btn");

toggleBtn.addEventListener("click", toggleMode);

you are setting up a new listener each time the function is run, even if the DOM is not updated. This could result in multiple listeners being registered and firing simultaneously.

You need to add your listener the react way.

function Component ( props ){
  const [ isFirst, setIsFirst ] = useState( true );
  const [ toggle, setToggle ] = useState( false );
  useEffect(() => {
    if( isFirst ) {
      setIsFirst( false );
      return;
    }

    document.documentElement.classList.toggle("dark");
  }, [ toggle ] );

  return <div>
    <button id="toggle-btn" onClick = { e => setToggle( !toggle ) } />
  </div>
}

CodePudding user response:

I resolved a similar problem in this post: Why does my NextJS Code run multiple times, and how can this be avoided?

Your code should only run once if you disable react strict mode.

  • Related