How do you call history.replace without scrolling to the top?

Viewed 270

I am using react-router and whenever I call history.replace, it scrolls to the top when all I want to do is update the url. How do you update the url without scrolling to the top?

2 Answers

Perhaps you're hard refreshing

If you make a hard refresh your app has to mount again and will put you at the top of the page. You should use the methods for pragmatic route change from the react router library.

Your body is mounting

If you change route and swap out your main scroll (should be body) it will put you at the top of the screen.

To Stop this make sure you're body is outside of the router.

If someone is still facing this issue with React-router-dom, there's a prop in the <Router /> component that can be set to true to turn it off.

disableScrollToTop={true}

Related