I'm trying to load all the images before removing the LOADING SCREEN using this code. The loading works and it goes away when all the images are loaded, But When I try to use the images as background for a div, it takes time to load it again. I'll appreciate your advice
pages/index.tsx:
export default function Home() {
const [isLoaded, setIsLoaded] = useState(false);
useEffect(() => {
const images = [
"/images/homePage/main-illustration.webp",
"/images/homePage/title-logo-text.webp",
"/images/homePage/title-logo-gear.webp"
];
let loadedCount = 0;
const checkIfAllLoaded = () => {
loadedCount++;
if (loadedCount === images.length) {
setIsLoaded(true);
}
};
images.forEach((src) => {
const img = new Image();
img.onload = checkIfAllLoaded;
img.src = src;
});
}, []);
return (
<>
{!isLoaded && (
<div>
Loading...
</div>
)}
{isLoaded && (
<>
<Navbar />
<HomePage />
<Footer />
</>
)}
</>
);
}
components/Hero.tsx
const Hero = () => {
return (
<div className="bg-[url(/images/homePage/main-illustration.webp)] bg-cover">
</div>
);
};
export default Hero;