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

175
Views
Can I change the state for given time and then revert it back to the original state using settimeout

I want to get the value to true for one second, after which it will revert back to false using settimeout but stuck. Here is my code:

const [value, setValue] = useState(false)
const handleClick = () => {
    setTimeout(() => {
        setValue(!value)
        console.log(value);
    },1000);
    console.log(value);
}
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

const [value, setValue] = useState(false);
const timeoutId = useRef(null);

const handleClick = () => {
    // clear existing timeout
    clearTimeout(timeoutId.current);
    // set the value to true
    setValue(true);
    // revert the value to false after 1s
    timeoutId.current = setTimeout(() => setValue(false), 1000);
}
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!