I am trying to fix the scrolling issue in React wherein when I was to scroll down to a certain point on the page, and then navigates to another page, that page will begin at the same scroll point as the previous page.
Here's what I tried:
import { useLocation } from 'react-router'
const ScrollToTop: React.FC = () => {
const { pathname } = useLocation()
React.useEffect(() => {
console.log('im scrolling')
window.scrollTo(0, 0)
}, [pathname])
return null
}
export default ScrollToTop
And then imports it like this:
import React from 'react'
import ReactDOM from 'react-dom'
import { BrowserRouter } from 'react-router-dom'
import ScrollToTop from './components/base/ScrollToTop'
import Apps from './App'
ReactDOM.render(
<BrowserRouter>
<ScrollToTop />
<Apps />
</BrowserRouter>,
document.getElementById('root'),
)
Im scrolling is printing in the console but the window.scrollTo is not working.
I also tried the useLayoutEffect, but the same result still not scrolling.