Created
October 1, 2021 06:39
-
-
Save Dearest/062c6d110c0ecffe73c1c7124da2d5f5 to your computer and use it in GitHub Desktop.
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 { | |
| useState, useRef, useEffect, | |
| } from 'react'; | |
| import { useRouter } from 'next/router'; | |
| import { useEventCallback } from '@material-ui/core/utils'; | |
| export default function usePageRefresh(callback, { skip } = {}) { | |
| const router = useRouter(); | |
| const [refreshing, setRefreshing] = useState(false); | |
| const currentAsPath = useRef(); | |
| currentAsPath.current = router.asPath; | |
| const handleRouterChangeStart = useEventCallback(async (url) => { | |
| if (currentAsPath.current === url && !refreshing) { | |
| setRefreshing(true); | |
| await callback(); | |
| setRefreshing(false); | |
| } | |
| }); | |
| useEffect(() => { | |
| if (skip) return undefined; | |
| router.events.on('routeChangeStart', handleRouterChangeStart); | |
| return () => { | |
| router.events.off('routeChangeStart', handleRouterChangeStart); | |
| }; | |
| }, [handleRouterChangeStart, router.events, skip]); | |
| return { refreshing }; | |
| } | |
| // const {refreshing} = usePageRefresh(handlePageRefresh); | |
| // <TaskGridTable | |
| // ref={gridTableRef} | |
| // loading={refreshing} | |
| // className={classes.taskGridTable} | |
| // uuid={memberUuid} | |
| // quickFilterTerm={quickFilterTerm} | |
| // queryDataKey="memberTasks" | |
| // editable | |
| // memberTasksProps={{ | |
| // memberId: memberSgid, | |
| // memberUuid, | |
| // }} | |
| // /> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment