I want to show a spinner every time someone on my site changes the page.
I tried to do it this way
import React, { useEffect, useState } from 'react';
import { useLocation } from 'react-router-dom';
import "./Spinner.css"
export function Spinner() {
const { pathname } = useLocation();
const [isLoading, setIsLoading] = useState(true);
useEffect(() => {
const body = document.querySelector("body");
body.style.overflow = "hidden";
window.addEventListener("load", () => {
body.style.overflow = "auto";
setIsLoading(false);
}, { once: true });
}, [pathname])
return (
isLoading ?
<div className="spinner-wrapper">
<div class="spinner">
Loading
<div class="spinner-sector spinner-sector-red" ></div>
<div class="spinner-sector spinner-sector-blue"></div>
<div class="spinner-sector spinner-sector-green"></div>
</div>
</div>
: ""
)
}
But it seems that the window is already loaded and the callback function for the event listener is not invoked, but actually, my images aren't loaded, yet! I also tried to attach the event listener for the body or the root element but the result is the same.