Skip to content

Instantly share code, notes, and snippets.

@rewop
Created May 20, 2016 16:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rewop/d43fb0168fd7fd6a4f01d2108859617e to your computer and use it in GitHub Desktop.
Save rewop/d43fb0168fd7fd6a4f01d2108859617e to your computer and use it in GitHub Desktop.
const handleScroll = _history => {
_history.listen(location => {
// Use setTimeout to make sure this runs after React Router's own listener
setTimeout(() => {
// Keep default behavior of restoring scroll position when user:
// - clicked back button
// - clicked on a link that programmatically calls `history.goBack()`
// - manually changed the URL in the address bar (here we might want
// to scroll to top, but we can't differentiate it from the others)
if (location.action === 'POP') {
return;
}
// In all other cases, scroll to top
window.scrollTo(0, 0);
}, 50);
});
};
handleScroll(history);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment