페이지 새로고침 시 예상치 못한 스크롤 위치로 이동하는 문제
이러한 현상이 발생하는 이유는 브라우저의 scrollRestoration
기능 때문입니다.
이 기능은 UX를 개선하기 위해 도입되었으며 다음과같이 작동합니다.
1useEffect(() => { 2 if ("scrollRestoration" in window.history) { 3 window.history.scrollRestoration = "manual" 4 } 5 window.scrollTo(0, 0) 6 }, [])
이 코드는 크게 두가지 역할을 수행합니다.
1if ("scrollRestoration" in window.history) { 2 window.history.scrollRestoration = "manual" 3}
scrollRestoration
속성을 manual
로 설정하여 브라우저의 자동 스크롤 복원 기능을 비활성화1window.scrollTo(0, 0)
https://developer.mozilla.org/ko/docs/Web/API/History/scrollRestoration