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

125
Visualizações
Returning an image from Firebase storage within Reactjs

I have a function which goes off to retrieve an image from Firebase Storage:

import {
  getStorage,
  ref,
  getDownloadURL
} from 'firebase/storage';

const storage = getStorage();

export async function getImage(location) {
  const ImageURL = await getDownloadURL(ref(storage, location));
  return await ImageURL;
}

This is then called within a React component:

import { getImage } from 'API/Storage/getImage';

const EventPage = (props) => {
  return (
    <div>
      <img src={getImage('image.jpg')}
    </div>
  );
}

export default EventPage;

However the image doesn't display and instead shows: <img src="[object Promise]" alt="Show Image">

What's the best way of resolving the promise? Console logging ImageURL within the getImage function returns the URL as expected.

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

0

You need to wait for the promise to resolve. You can fetch the data in a useEffect and store the value in a useState.

Here's a basic example:

import { getImage } from 'API/Storage/getImage';

const EventPage = (props) => {
  const [image, setImage] = useState();

  useEffect(async () => {
    const image = await getImage('image.jpg');
    setImage(image);
  }, []);

  return (
    <div>
      {image ? <img src={image} /> : "Loading image"}
    </div>
  );
}

export default EventPage;

The useEffect in the above example doesn't account for the case when your component is unmounted while still fetching the data. An implementation that accounts for that looks like so:

useEffect(() => {
  let wasUnmounted = false;
  getImage('image.jpg').then((image) => {
    if (wasUnmounted) return;
    setImage(image);
  });
  return () => {
    wasUnmounted = true;
  };
}, []);

Notice that the callback provided to useEffect isn't async. That's because async functions return a promise. To be able to detect that the component was unmounted we need to return a callback (it will be called when the component is unmounted, because of the empty array passed as a second argument). Please read more about useEffect here: https://reactjs.org/docs/hooks-reference.html#useeffect

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