I'm trying not to write all my code in the same file, and to extract commonly used functions like downloading an image from Firebase to separate functions/files.
I have two functions in React named
OfferCard
(renders a card based on props)getImage
(fetches an image's download-URL from Firebase Storage, to provide an image to the OfferCard component).
I'm trying to fetch an image file from getImage
and display that image in the returned <CardMedia />
image property from the OfferCard
functional component.
The problem I'm facing is, getImage doesn't fetch the data and return it in time for the setOfferImage
. Meaning the state variable offerImage
remains null
or undefined
.
In the console, I can see that the getImage
is console logging the value AFTER the OfferCard
console logs the value/.
I believe it's an issue with asynchronous code and promises but am not too sure how to resolve this one.
OfferCard.js:
import getImage from '../utils/helperFunctions/getImage';
export default function OfferCard(props) {
const classes = useStyles();
const [offerImage, setOfferImage] = React.useState(null)
useEffect(() => {
if (props.image) {
initFirebase();
setOfferImage(getImage(props.image));
console.log(offerImage)
}
}, [])
return (
<Link href={`/offers/${props.id}`}>
<Card className={classes.card} >
{offerImage ?
<CardMedia
image={offerImage}
/> : <CircularProgress/>
}
</Card>
</Link>
)
}
getImage:
export default function getImage(path) {
let storage = firebase.storage();
let pathReference = storage.ref(`${path}.png`);
pathReference.getDownloadURL()
.then((url) => {
console.log("returning", url);
return (url);
})
.catch((error) => {
// Handle any errors
console.log("Could not get image: ", error);
});
}