Last active
March 7, 2018 11:28
-
-
Save Gurenax/5f262ccd84418e466bebbe8b20c6be98 to your computer and use it in GitHub Desktop.
Enable scroll to top on every Route in react-router
This file contains 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
import { Component } from 'react' | |
import { withRouter } from 'react-router' | |
import PropTypes from 'prop-types' | |
class ScrollToTop extends Component { | |
componentDidUpdate(prevProps) { | |
if (this.props.location !== prevProps.location) { | |
window.scrollTo(0, 0) | |
} | |
} | |
render() { | |
return this.props.children | |
} | |
} | |
ScrollToTop.propTypes = { | |
match: PropTypes.object.isRequired, | |
location: PropTypes.object.isRequired, | |
history: PropTypes.object.isRequired | |
} | |
export default withRouter(ScrollToTop) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment