mirror of
https://github.com/geoffsee/open-gsio.git
synced 2025-09-08 22:56:46 +00:00
init
This commit is contained in:
26
src/layout/usePageLoaded.ts
Normal file
26
src/layout/usePageLoaded.ts
Normal file
@@ -0,0 +1,26 @@
|
||||
import { useEffect, useState } from "react";
|
||||
|
||||
const usePageLoaded = (callback: () => void) => {
|
||||
const [isLoaded, setIsLoaded] = useState(false);
|
||||
|
||||
useEffect(() => {
|
||||
const handlePageLoad = () => {
|
||||
setIsLoaded(true);
|
||||
callback();
|
||||
};
|
||||
|
||||
if (document.readyState === "complete") {
|
||||
// Page is already fully loaded
|
||||
handlePageLoad();
|
||||
} else {
|
||||
// Wait for the page to load
|
||||
window.addEventListener("load", handlePageLoad);
|
||||
}
|
||||
|
||||
return () => window.removeEventListener("load", handlePageLoad);
|
||||
}, [callback]);
|
||||
|
||||
return isLoaded;
|
||||
};
|
||||
|
||||
export default usePageLoaded;
|
Reference in New Issue
Block a user