functional component - react js I am running a process . I want to stop that process if user click on stop (stop button is on a diffrent component - Modal )
const [terminate, setterminate] = useState(false);
const process =()=>{
... some code (has no use of terminate or setterminate)
for(let i=0;i<5;i++){
if(terminate){
break
}
// api call
}
} // process function ends
const stopProcess = ()=>{
setterminate(true)
}
// called a modal and passes stopProcess as prop. and when click stop button in modal, that invoke stopProcess function. but for loop insite process function dont get break
...
<button onClick={stopProcess}></ button >
...
everything works fine, except for does not breaks. function is invoked , tested by console.log("sdf")
(clousers and working of useState (async ) and how it is update. (please read that first)) to get immediately value of useState
let temp_terminate=false;
setterminateProcess((state)=>{
temp_terminate=state;
return state;
})