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

95
Visualizações
Length is showing 0 even after using async and await keyword

I Dont know what is going on but even after using async and await keyword still the length is showing zero. Thanks in advance.

const commercial_shoots = [];
  let test;
  React.useEffect(() => {
    async function fetchData() {
      const app_ref = ref(storage, "Home/");
      await listAll(app_ref)
        .then((res) => {
          res.items.forEach((itemRef) => {
            getDownloadURL(itemRef).then((url) => {
              commercial_shoots.push({ img: url });
            });
          });
        })
        .catch((error) => {
          console.log(error);
        });
    }
    fetchData();
  }, []);
  return <div>{commercial_shoots.length}</div>;
};
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

React only re-renders the component when state or props updates. Here, you are only updating a local variable. So, even when it updates, the UI does not reflect the change.

The solution would be to use commercialShoots as a state in the component.

const CommercialShoots = () => {
  const [commercialShoots, setCommercialShoots] = useState([]);
  useEffect(() => {
    async function fetchData() {
     try {
      const app_ref = ref(storage, "Home/");
      const res = await listAll(app_ref);
      const downloadUrls = await Promise.all(res.items.map(itemRef) => getDownloadURL(itemRef));
      const mappedUrlsToImg = downloadUrls.map((url) => ({ img: url });
      setCommercialShoots(mappedUrlsToImg);
     } catch (error) {
      console.error(error);
     }
    }
    fetchData();
  }, []);
  return <div>{commercial_shoots.length}</div>;
};

NOTE - Since we are using async / await extensively, I took the liberty of updating the .then() to async / await syntax.

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