Skip to content

Instantly share code, notes, and snippets.

@teknosains
Last active January 30, 2025 09:52
Show Gist options
  • Save teknosains/ba83769a5c1f4dcc852c194c45240099 to your computer and use it in GitHub Desktop.
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
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