Skip to content

Commit

Permalink
fix: 스크롤 복원 버그
Browse files Browse the repository at this point in the history
  • Loading branch information
wildcatco committed Aug 3, 2023
1 parent fbf9da0 commit e23fbb5
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 15 deletions.
5 changes: 0 additions & 5 deletions src/components/user/SelectMyPostType.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
import { useSetAtom } from 'jotai';

import { PostType } from '@/pages/user/UserPage';
import { scrollRestorationAtom } from '@/states/scroll-restoration';

interface SelectMyPostTypeProps {
postType: PostType;
Expand All @@ -14,8 +11,6 @@ export default function SelectMyPostType({
switchPostType,
postingCount,
}: SelectMyPostTypeProps) {
const setScrollRestoration = useSetAtom(scrollRestorationAtom);

return (
<div className="mx-[2.5rem] mb-[2.1rem] flex justify-between space-x-[1.2rem] overflow-hidden rounded-[2.35rem] bg-background-subBg-100">
{['내 게시글', '참여한 게시글'].map((label) => {
Expand Down
21 changes: 11 additions & 10 deletions src/hooks/useScrollRestoration.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,13 @@ export default function useScrollRestoration<U extends HTMLElement>(
);
const ref = useRef<U | null>(null);

useEffect(() => {
setScrollRestoration((prevScrollRestoration) => ({
...prevScrollRestoration,
[key]: 0,
}));
}, []);

useEffect(() => {
if (!ref.current) return;

Expand All @@ -19,16 +26,10 @@ export default function useScrollRestoration<U extends HTMLElement>(

const height = ref.current.scrollTop;

if (!scrollRestoration) {
setScrollRestoration({
[key]: height,
});
} else {
setScrollRestoration((prevScrollRestoration) => ({
...prevScrollRestoration,
[key]: height,
}));
}
setScrollRestoration((prevScrollRestoration) => ({
...prevScrollRestoration,
[key]: height,
}));
};

ref.current.addEventListener('scroll', handleScroll);
Expand Down

0 comments on commit e23fbb5

Please sign in to comment.