Skip to content

Instantly share code, notes, and snippets.

@smashercosmo
Created August 16, 2018 17:14
Show Gist options
  • Save smashercosmo/bf0976de33d74e76c66902b243a598eb to your computer and use it in GitHub Desktop.
Save smashercosmo/bf0976de33d74e76c66902b243a598eb to your computer and use it in GitHub Desktop.
import React from 'react'
import PropTypes from 'prop-types'
import withRouter from 'react-router-dom/es/withRouter'
class ScrollManager extends React.Component {
static propTypes = {
history: PropTypes.object.isRequired,
location: PropTypes.object.isRequired,
}
componentWillReceiveProps(nextProps) {
const { pathname } = this.props.location
const { pathname: nextPathname } = nextProps.location
const { action: nextAction } = nextProps.history
if (
(nextAction === 'PUSH' || nextAction === 'REPLACE') &&
pathname !== nextPathname
) {
window.scrollTo(0, 0)
}
}
render() {
return null
}
}
export default withRouter(ScrollManager)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment