Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

108
Vistas
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
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda