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?
<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?
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?