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

77
Views
Why `setTimeout` call more than one time when I use `useState`?

I'm so confused about useState in React hooks.

I do not know why console.log in setTimeout function calls more than one time when I use useState.

If I remove useState it normally calls only once. And If I use Class state instead hooks, it normally calls only once as well.

Why is it happened that ? And how can I handle it ?

(here is my code)

import React, { useState, useEffect } from "react";
import "./App.css";

const usePassword = () => {
  const [passwordValue, setPasswordValue] = useState({
    password: "",
    passwordHidden: "",
  });
  let timer = null;

  const trigger = () => {
    clearTimeout(timer);
    timer = setTimeout(() => console.log("end"), 1000);
  };

  const onPasswordChanged = (name, value) => {
    setPasswordValue((prev) => ({ ...passwordValue, passwordHidden: value }));
    trigger();
  };

  return { passwordValue, onPasswordChanged };
};

function App() {
  const { passwordValue, onPasswordChanged } = usePassword();

  const onChanged = (event) => {
    const { name, value } = event.target;
    onPasswordChanged(name, value);
  };
  const onSubmit = () => {
    console.log("submitted!", passwordValue);
  };
  return (
    <div className="App">
      <header className="App-header">
        <input name="password" onKeyUp={onChanged} />
        <button onClick={onSubmit}>Submit</button>
      </header>
    </div>
  );
}

export default App;


about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Whenever you set the state using useState you get a new timer variable, as the function is called again. This is why your clearTimeout does not work.

You can use a ref to hold on to the value between render cycles:

 const timer = useRef(null);

  const trigger = () => {
    clearTimeout(timer.current);
    timer.current = setTimeout(() => console.log("end"), 1000);
  };

Edit competent-goldstine-jbt3t

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!