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

145
Views
How to update state when the api returns previous value using javascript?

i have to retrieve a value from api and store it in a state. but the value returned from run api is always previous value of the run.

so consider i have 2 buttons run1 and run2. clicking either of them will call the same run api for that item.

when run1 is clicked isComplete is true from api and for run2 isComplete is false from api.

when none of the buttons run1 and run2 clicked the data returned is undefined for the item and hence isComplete is undefined too. the data is like below,

data = [ 
           {
               isCompleteRun: true,//this can be true or false
           }
       ]

now if user clicks run1 btn i get runinprogress true. and isComplete: true, if run2 btn clicked runinprogress is true and isComplete is false.

i have to show two div elements based on value of isCompleteRun.

{isRunInProgress && isCompleteRun && <div>first</div>}
{isRunInProgress && !isCompleteRun && <div>second</div>}

but the problem is isCompleteRun is the previous value unless either of run button clicked. how can i save the isCompleteRun to state and have updated value every time run clicked.

below is the code,

const isCompleteRun = React.useMemo(() => {
    return data?[0]?.isCompleteRun;
}, [data]);

the value of isCompleteRun is always the value of previous data for sometime unless run button is clicked or undefined when no run button clicked for the first time. could someone help me fix this. thanks.

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