How to check if all images are loaded in a particular component in next.js 13 (app router)?

125 views Asked by At

Next.js provides onLoadingComplete which can be used to check if a particular image is loaded or not.

Question

What if I have a couple of images in a component and I wish to check if all the images are loaded or not!

Promise.all(Array.from(document.images).map(img => {
if (img.complete)
    return Promise.resolve(img.naturalHeight !== 0);
return new Promise(resolve => {
    img.addEventListener('load', () => resolve(true));
    img.addEventListener('error', () => resolve(false));
});
})).then(results => {
if (results.every(res => res))
    console.log('all images loaded successfully');
else
    console.log('some images failed to load, all finished loading');
});

listening to all image load in legacy js

0

There are 0 answers