Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

229
Views
Wait until all images are loaded in React.JS

How can I wait until all images are loaded in React.JS?

The problem is that I do some calculations (image processing) using those images and the results are different between multiple refreshes of the browser because at certain times the calculations start before the images are 100% loaded. Is there a way to wait until 100% of all images are loaded?

I've tried something like this:

const [imagesLoaded, setImagesLoaded] = useState(false);
{images.map((image, index) => {
   return <ShowImage source={image} index={index+1} key={index} onLoad={() => setImagesLoaded(index)} />
})}
const ShowImage: React.FC<{source:string, index: number, onLoad: () => void}> = ({source, index, onLoad}) => {
    return (
        <img src={source} width="640" height="480" id={'image' + index} alt={'image' + index} onLoad={onLoad}/>
    )
}

And the 'checker':

useEffect(() => {
   if(imagesLoaded === 5) {
      ... do something
   }
}, [imagesLoaded]);

But the problem is that this is working only for the first page render, if I refresh is not working, but I refresh one more time is working again and sometimes needs more refreshes, what's the problem?

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

Your images might not load in the intended order. If your 4th image loads after the 5th one, then the value of imagesLoaded will be 4 at the end. To prevent that, I would increment the value one at a time, so when all five images are loaded the value will be 5.

onLoad={() => setImagesLoaded(v => v + 1)} 
about 4 years ago · Juan Pablo Isaza Report

0

You could refactor your ShowImage component to only set imagesLoaded when the last image source URL is loaded.

function ShowImages({ urls, setImagesLoaded }) {
  const onLoad = (index) => {
    if (index === urls.length - 1) {
      setImagesLoaded(true)
    }
  };
  return (
    <>
      {urls.map((url, index) => (
        <img src={url} onLoad={() => onLoad(index)} key={url} />
      ))}
    </>
  );
}
export default ShowImages;

And use the component something like this

const [imagesLoaded, setImagesLoaded] = useState(false);
...

useEffect(() => {
   if(imagesLoaded) {
      ... do something
   }
}, [imagesLoaded]);

...
<ShowImages urls={images} setImagesLoaded={setImagesLoaded}/>

Working example here

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!