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

146
Visualizações
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
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