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

127
Visualizações
Variables updated in fetch method don't retain in state
  const searchCacheFlightHotelsPackage = () => {
setValues({ ...values, loading: true });
let result = null;
getCacheFlightHotelsPackage() //returns a promise
    .then((data) => {
      if (data.list.length > 0) {
        result = paginate(data.list);
        hotelFlightPackageList = data.list;
      } else {
        console.log("sorry no packages found========");
      }
      console.log(hotelFlightPackageList);
      setValues({ ...values, loading: false });
    })
    .catch((e) => {
      console.log("packages data error=======", e);
      setValues({ ...values, loading: false });
    });

I have variables outside this function like let hotelFlightPackageList = []; which updates in the method but once outside the method it's back to being an empty array. I have also tried using setValues which you can see an example of with the loading setting to false. I wish I could just update the global state in fetch but It gives a react hook error when doing that.

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

0

Turns out you can update the global state in fetch once you make the global state variable a const outside of the fetch! Working now!

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