Created
May 20, 2016 16:28
-
-
Save rewop/d43fb0168fd7fd6a4f01d2108859617e to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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