Not exactly sure, but it seems like it’s because you aren’t storing all of them, just iterating through indexes in the for loop, so the value will be the last link it downloaded?
In your code snippets, I don’t see where it is pushing, slicing, unshifting , mapping or anything like that to a declared empty array like myLinks = []; or something, so I’m assuming it is just returning the last link that it downloaded. (Iterating through but not storing each on the client side?).
Maybe you are doing that somewhere, but didn’t post that part?
I have tried to make an array containing all the “a” tags and then iterate through it and run link.click(). But same result
function handleDownload(){
console.log(imageUrls)
for(let i=0;i<imageUrls.length;i++){
//console.log(imageUrls[i])
createLink(imageUrls[i])
}
clickLinksToDownload()
//after downloading all the images clears imageUrl and images state
clearImages()
clearImageUrls()
}
// downloads an image from the server
function createLink(imageLink){
let link=document.createElement('a');
link.href = process.env.REACT_APP_PROXY+"/download/"+imageLink
link.download = imageLink;
setDownloadLinks(prevState=>{
prevState.push(link)
return prevState;
})
// console.log(downloadLinks)
}
function clickLinksToDownload(){
downloadLinks.forEach(link=>{
console.log(link)
link.click()
})
}