From 5fc1d26243e977c7aec75ade622c6df4cf90575e Mon Sep 17 00:00:00 2001 From: DrMint Date: Mon, 29 Aug 2022 00:11:49 +0200 Subject: [PATCH] Hopefully fixed the scrool to anchor when changing page --- src/hooks/useScrollIntoView.ts | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/src/hooks/useScrollIntoView.ts b/src/hooks/useScrollIntoView.ts index 34de9a7..4f5a504 100644 --- a/src/hooks/useScrollIntoView.ts +++ b/src/hooks/useScrollIntoView.ts @@ -1,12 +1,12 @@ import { useRouter } from "next/router"; -import { useEffect } from "react"; -import { useBoolean, useCounter } from "usehooks-ts"; +import { useEffect, useState } from "react"; +import { useCounter } from "usehooks-ts"; import { isDefined } from "helpers/others"; export const useScrollIntoView = (): void => { const router = useRouter(); const { count, increment } = useCounter(0); - const { value: hasReachedElem, setTrue: setHasReachedElem } = useBoolean(); + const [hasReachedElem, setHasReachedElem] = useState(false); useEffect(() => { if (!hasReachedElem) { const indexHash = router.asPath.indexOf("#"); @@ -17,7 +17,7 @@ export const useScrollIntoView = (): void => { if (isDefined(element)) { console.log(`[useScrollIntoView] ${hash} found`); element.scrollIntoView(); - setHasReachedElem(); + setHasReachedElem(true); } else { console.log(`[useScrollIntoView] ${hash} not found`); setTimeout(() => { @@ -27,4 +27,6 @@ export const useScrollIntoView = (): void => { } } }, [increment, router.asPath, count, hasReachedElem, setHasReachedElem]); + + useEffect(() => setHasReachedElem(false), [router.asPath]); };