Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

158
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda