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

111
Views
useState Hook does not pass to other functions

In my component i have a Button which increases the counter useState hook.

const Component = () => {
  const [count, setCount] = useState(0)

  let cd = 5
  let result = 25

  const counterHandler = () => {
    document.getElementById('count').innerHTML = count
    setCount(count + 1)
  }

  const countDown = () => {    
    let interval = setInterval(() => {
      if (cd > 0) {
        cd -= 1
        document.getElementById('countDown').innerHTML = cd
      } else if (cd === 0) {
        checkAnswer()
        clearInterval(interval)
      }
   }, 1000)
 
   const checkAnswer = () => {
     if (count > resultat) {
       document.getElementById('message').innerHTML = 'Yess!'
     } else {
       document.getElementById('message').innerHTML = 'Wrong!!'
       setTimeout(() => {
         document.getElementById('message').innerHTML = ''
       }, 2000)
     }
   }

  return (
    <div>
      <h1 id='count'>
        {count}
      </h1>
      <h4 id='countDown'>
        0
      </h4>
      <button onClick={counterHandler}>Push Me!</button>
      <button id='go-btn' onClick={countdown}>Go</button>
      <h4 id='message'>
      </h4>  
    </div>
  )
}

After a countdown the count is checked if it's the same as in the result const. However, if I log the count in the checkAnswer function, it shows always a count of 0. Therefor the result is always wrong, no matter how many times the counterHandler- button get pressed. What am I doing wrong?

about 4 years ago · Juan Pablo Isaza
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!