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

147
Visualizações
React state not updating inside a for loop

I'm trying to run a for loop in a React app which will run through a user defined range of tokens and place bids based on i of the for loop. Sounds very simple, and it is.

However I'm having an issue where I've added a block to check whether "isBidding" changes to false, which should then break the for loop. isBidding is a state variable local to the component.

It seems like when the for loop starts, isBidding will always be true, even when the state is changed to false whilst the loop is running. I can't for the life of me figure out how to ensure the global state variable isBidding gets updated within a for loop.

for (let i = startTokenId; i < endTokenId + 1; i++) {
  // Inside the loop we try to place bids on each token, waiting for user action
  setBidderScanningString(`Bidding on token id #${i} (${i} of ${endTokenId})...`);
  // Try to place the bid
  try {
    await createBuyOrder(i);
  } catch (e) {
    parseBidError(e);
  }
  // Check if loop is over, if so set bidding to false and end
  if (i.toString().toLowerCase() === endTokenId.toString().toLowerCase()) {
    setBidderScanningString('Bidding finished...');
    setIsBidding(false);
    return;
  }
  // Check whether isBidding has been set to false
  if (!isBidding) {
     return;
  }
}

From within the loop I have tried to call a function, which returns the latest isBidding state, however this always evaluates to true.

I have tried to add isBidding in the 2nd condition of the for loop however it still always evaluates to true. I have also tried to add labels and nest it inside a while (isBidding) loop however it still always evaluates to true.

I have also tried moving this code into a useEffect with a dependency on isBidding however the for loop still always returns true.

Is there something blindingly obvious I'm missing?

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

It seems that you are using useState setter setIsBidding and getter isBidding. JavaScript is single-threaded, and the for-loop runs synchronously, which just means that the entire loop will finish before the effects of calling setIsBidding are reflected in isBidding.

I would suggesting declaring a value locally for use inside the for loop, and then after the loop is done you can save the value to setIsBidding if desired.

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