0

I am trying to fetch images from Firebase storage and then display them in my react app. Currently, there are 4 images in my storage and want to display all.

enter image description here

Fetch 4 different images from storage properly but displayed first image(1.png) 4 times.

useEffect(() => {
    const fetchImages = async () => {

    let result = await storageRef.child('images').listAll();
        let urlPromises = result.items.map(imageRef => imageRef.getDownloadURL());
    
        return Promise.all(urlPromises);
    }
    
    const loadImages = async () => {
        const urls = await fetchImages();
        setFiles(urls);
    }
    loadImages();
    }, []);

am I using map wrong?

 <div>
   {files.map((index) => (
      <img key={index} src={files} />
   ))}
 </div>

1
  • <img key={index} src={index} />, setting src to files incorrect Commented Nov 9, 2020 at 2:52

1 Answer 1

3

You should set each array element.

 <div>
   {files.map((file, index) => (
      <img key={'unique_key_string' + index} src={file} />
   ))}
 </div>
Sign up to request clarification or add additional context in comments.

1 Comment

Oh! it's worked! Thank you so much. I made simple mistake.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.