0
votes

I have to issue in displaying the image in react. List of all Image urls in get from firebase storage.Passing all the urls display the image shows urls.map is not a function

const ViewImages = () => {

var storageRef = firebase.storage().ref("images/");

const [image, setImage] = useState(""); const [urls, setFiles] = useState("");

const [imageUrl, setImageUrl] = useState([]);

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

let result = await storageRef.child('images1/').listAll();
    let urlPromises = result.items.map(imageRef => imageRef.getDownloadURL());
 
    return Promise.all(urlPromises);
    //console.log(urlPromises);

}

const loadImages = async () => {
    const urls = await fetchImages();
    setFiles(urls);
    console.log(urls[0]);
}
loadImages();
}, []);

return(

      {urls.map((urls,i) => (
          <img src={urls} key={i}/>
      ))} 
     
  </div>

);

}

export default ViewImages;

1

1 Answers

1
votes

The issue is you're calling the map method on an empty string ('') which is the initial value of urls. Set it to an empty array instead.

const [urls, setFiles] = useState([])