From 74abb00ec129c98bfc67b40143ccc04d4b799ecb Mon Sep 17 00:00:00 2001 From: franco sanchez Date: Wed, 14 Aug 2024 10:30:19 -0300 Subject: [PATCH] fix: problem with useScrollRestoration hook and other changes --- src/components/Footer.tsx | 3 +-- src/components/forms/NewBook.tsx | 1 + src/components/nav/DesktopNav.tsx | 2 +- src/hooks/useScrollRestoration.ts | 13 +++++++------ src/pages/Home.tsx | 3 +++ src/pages/Search.tsx | 4 ++-- src/pages/profile/Profile.tsx | 2 ++ 7 files changed, 17 insertions(+), 11 deletions(-) diff --git a/src/components/Footer.tsx b/src/components/Footer.tsx index e04a133..2dede35 100644 --- a/src/components/Footer.tsx +++ b/src/components/Footer.tsx @@ -64,9 +64,8 @@ export function Footer() { return ( <> - + { + useLayoutEffect(() => { function handleScroll() { - sessionStorage.setItem(location.key, JSON.stringify(window.scrollY)); + sessionStorage.setItem(location.pathname, JSON.stringify(window.scrollY)); } window.addEventListener('scroll', handleScroll); - const scrollPosition = sessionStorage.getItem(location.key); + const scrollPosition = sessionStorage.getItem(location.pathname); + if (scrollPosition && !loading) { window.scrollTo(0, JSON.parse(scrollPosition)); } @@ -19,5 +20,5 @@ export function useScrollRestoration(loading) { return () => { window.removeEventListener('scroll', handleScroll); }; - }, [location.key, loading]); + }, [location.pathname, loading]); } diff --git a/src/pages/Home.tsx b/src/pages/Home.tsx index 9fd4d0f..1a24e13 100644 --- a/src/pages/Home.tsx +++ b/src/pages/Home.tsx @@ -48,6 +48,7 @@ export function Home() { urlImage='https://xbu.netlify.app/ogImage.png' /> { let isMounted = true; diff --git a/src/pages/profile/Profile.tsx b/src/pages/profile/Profile.tsx index 95af195..86083b9 100644 --- a/src/pages/profile/Profile.tsx +++ b/src/pages/profile/Profile.tsx @@ -195,6 +195,7 @@ export function Profile() { urlImage={data?.pages[0].user.picture} />