Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

92
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda