Skip to content

Commit

Permalink
format
Browse files Browse the repository at this point in the history
  • Loading branch information
jo-inge-arnes committed Sep 20, 2024
1 parent 860b6cc commit 4321815
Showing 1 changed file with 12 additions and 5 deletions.
17 changes: 12 additions & 5 deletions packages/qmongjs/src/helpers/hooks/useShouldReinitialize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ const anyQueriesLoading = (queries: UseQueryResult<any, unknown>[]) => {
* The variable from useShouldReinitialize becomes true either:
* 1) The first time the page has finished "pre-rendering" and the API calls have already finished loading. At this point, the query/get parameters from the URL are also ready.
* 2) The first time the API calls finish loading and the page is already "pre-rendered". That is, pre-rendering finishes first and then the API calls. At this point, the query/get parameters from the URL are also ready.
*
*
* @param queries An array of tanstack queries of type UseQueryResults
* @returns True the first time the page is finished hydrated and all queries have finished loading
*/
Expand All @@ -22,9 +22,12 @@ export default function useShouldReinitialize(
) {
const router = useRouter();

const [previousPrerenderingStatus, setPreviousPrerenderingStatus] = useState(router.isReady);
const [previousPrerenderingStatus, setPreviousPrerenderingStatus] = useState(
router.isReady,
);

const prerenderingJustCompleted = previousPrerenderingStatus !== router.isReady;
const prerenderingJustCompleted =
previousPrerenderingStatus !== router.isReady;

useEffect(() => {
setPreviousPrerenderingStatus(router.isReady);
Expand All @@ -36,11 +39,15 @@ export default function useShouldReinitialize(
areQueriesStillLoading,
);

const queriesJustCompleted = previousQueryLoadingStatus && !anyQueriesLoading(queries);
const queriesJustCompleted =
previousQueryLoadingStatus && !anyQueriesLoading(queries);

useEffect(() => {
setPreviousQueryLoadingStatus(areQueriesStillLoading);
}, [areQueriesStillLoading]);

return (prerenderingJustCompleted && !areQueriesStillLoading) || queriesJustCompleted;
return (
(prerenderingJustCompleted && !areQueriesStillLoading) ||
queriesJustCompleted
);
}

0 comments on commit 4321815

Please sign in to comment.