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

105
Views
useState Hook no pasa a otras funciones

En mi componente, tengo un botón que aumenta el gancho useState del counter .

 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> ) }

Después de una cuenta regresiva, se verifica si el count es el mismo que en el result constante. Sin embargo, si registro el count en la función checkAnswer , siempre muestra un count de 0. Por lo tanto, el resultado siempre es incorrecto, sin importar cuántas veces se presione el botón counterHandler -. ¿Qué estoy haciendo mal?

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!