mirror of
https://github.com/DefectingCat/DefectingCat.github.io
synced 2025-07-16 09:11:38 +00:00
Rename components name
This commit is contained in:
40
lib/hooks/use-router-loading.ts
Normal file
40
lib/hooks/use-router-loading.ts
Normal file
@ -0,0 +1,40 @@
|
||||
import { useRouter } from 'next/router';
|
||||
import { useCallback, useEffect, useState } from 'react';
|
||||
|
||||
/**
|
||||
* Loading state when router changed.
|
||||
* @returns
|
||||
*/
|
||||
const useRouterLoading = () => {
|
||||
const router = useRouter();
|
||||
const [loading, setLoading] = useState(false);
|
||||
|
||||
const handleStart = useCallback(
|
||||
(url: string) => {
|
||||
url !== router.pathname ? setLoading(true) : setLoading(false);
|
||||
},
|
||||
[router.pathname]
|
||||
);
|
||||
const handleComplete = useCallback(() => {
|
||||
setLoading(false);
|
||||
}, []);
|
||||
|
||||
useEffect(() => {
|
||||
router.events.on('routeChangeStart', handleStart);
|
||||
router.events.on('routeChangeComplete', handleComplete);
|
||||
router.events.on('routeChangeError', handleComplete);
|
||||
|
||||
// If the component is unmounted, unsubscribe
|
||||
// from the event with the `off` method:
|
||||
return () => {
|
||||
router.events.off('routeChangeStart', handleStart);
|
||||
router.events.off('routeChangeComplete', handleComplete);
|
||||
router.events.off('routeChangeError', handleComplete);
|
||||
};
|
||||
// eslint-disable-next-line react-hooks/exhaustive-deps
|
||||
}, []);
|
||||
|
||||
return { loading };
|
||||
};
|
||||
|
||||
export default useRouterLoading;
|
Reference in New Issue
Block a user