Last active
January 30, 2025 09:52
-
-
Save teknosains/ba83769a5c1f4dcc852c194c45240099 to your computer and use it in GitHub Desktop.
Adding Loading Indicator On Remix Parent Layout When the Loader (any loaders from childrens) Take Some times to load
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { Toaster, toast } from 'react-hot-toast'; | |
export default function M() { | |
const navigation = useNavigation(); | |
const location = useLocation(); | |
// we only wants the loading shows if the route navigations | |
// are not from a form Submissions | |
const isRequestLoading = navigation.state === "loading"; | |
const isRequestAFormSubmission = Boolean(navigation.formAction); | |
useEffect(() => { | |
let timeoutId: ReturnType<typeof setTimeout>; | |
if (isRequestLoading && !isRequestAFormSubmission) { | |
timeoutId = setTimeout(() => { | |
toast.loading('Memuat halaman...', { | |
duration: Infinity, | |
position: 'top-center', | |
}); | |
}, 1000); // show the loading if 1secs have passed | |
} | |
return () => { | |
clearTimeout(timeoutId); | |
toast.dismiss(); | |
}; | |
}, [isRequestLoading, isRequestAFormSubmission, location]); | |
return ( | |
<> | |
<Layout> | |
<Suspense fallback={<div>Loading...</div>}> | |
<Outlet /> | |
</Suspense> | |
</Layout> | |
</> | |
); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment