Skip to content

Instantly share code, notes, and snippets.

@Gurenax
Last active March 7, 2018 11:28
Show Gist options
  • Save Gurenax/5f262ccd84418e466bebbe8b20c6be98 to your computer and use it in GitHub Desktop.
Save Gurenax/5f262ccd84418e466bebbe8b20c6be98 to your computer and use it in GitHub Desktop.
Enable scroll to top on every Route in react-router
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