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

104
Views
useState not updating , after using set outside of function

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 )

process function on app.js looks like

 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

Modal.js

...

<button onClick={stopProcess}></ button >
...

everything works fine, except for does not breaks. function is invoked , tested by console.log("sdf")

how do I break that foor loop ?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

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

about 4 years ago · Juan Pablo Isaza Report
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!