Zustand 스토어에 데이터와 스크롤 위치를 저장하여 복원합니다.
const usePostStore = create((set, get) => ({
posts: [], // 불러온 데이터
scrollY: 0, // 스크롤 위치
fetchNextPage, // 다음 페이지 로드
setScrollY, // 스크롤 저장
}))const handleBeforeNavigate = () => {
setScrollY(window.scrollY);
};useEffect(() => {
if (posts.length > 0 && scrollY > 0) {
requestAnimationFrame(() => {
window.scrollTo(0, scrollY);
});
}
}, [posts.length, scrollY]);