Hasan Tareque
Hasan Tareque

Reputation: 1741

React router page load issue

React router page load always keep the scrolling position same. Is there a simple way to keep the scroll position in a certain position of the page?

Upvotes: 1

Views: 84

Answers (1)

Ajeet Shah
Ajeet Shah

Reputation: 19863

useEffect(() => {
  let unlisten = history.listen(({ location, action }) => {
    console.log(action, location.pathname, location.state);    
    window.scrollTo(0, 0)
  });
  return () => unlisten() 
}, [])

You can listen to history change and scroll to top, or any other position you want, at history change. You can keep this code in your first, say App, component.

Upvotes: 1

Related Questions