Created
August 16, 2018 17:14
-
-
Save smashercosmo/bf0976de33d74e76c66902b243a598eb to your computer and use it in GitHub Desktop.
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 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