Scroll Restoration
Doc
History API: Scroll Restoration
Code
React
function ScrollToTop({history}) {
if (window.history.scrollRestoration === 'auto') {
window.history.scrollRestoration = 'manual'
}
useEffect(() => {
const unlisten = history.listen(() => {
window.scrollTo(0, 0)
})
return () => {
unlisten()
}
}, [])
return null
}
export default withRouter(ScrollToTop)