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

156
Vistas
Next Run code until Button is clicked again

learning next and stuff and had a question I did not have any success on finding online.

So I have a button

<button onClick={toggle} className="common-btn">{buttonText}</button>

I am wanting this button to control how long a section of code is ran. So within the toggle method I update the state and that works just fine. I also run a while loop hoping that if the state ever changed it would break out of the while loop running the code like so

...
setRunning(!running)
if(running){
    while(running) 
    {
      console.log("Running")
    }
}

... 

The issue comes in since it never breaks out of the while loop so even if the state is false it will continue to run the original while loop. Does anyone have any better insight on how to go about this?

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

<button onClick={() =>toggle() } className="common-btn">{buttonText}</button>
setRunning(!running)
if(running){
     While(running) {
      console.log("Running")
      } 
}else{
 console.log("Not Running) 
} 

Wyh dont you type like this?

about 4 years ago · Juan Pablo Isaza Denunciar

0

 const [running, setRunning] = useState(true) 
const toggle = () => {
  setRunning(!running) 
} 
<button onClick={() =>toggle() } className="common-btn">{buttonText}</button>
    if(running){
          console.log("Running")
    }else{
     console.log("Not Running) 
    } 
Wyh dont you type like this? 
about 4 years ago · Juan Pablo Isaza Denunciar
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