Last active
June 25, 2018 21:42
-
-
Save chris-kobrzak/708f31976be5fac2aa265816d6a03f2a to your computer and use it in GitHub Desktop.
Finessed variant of ScrollToTop from react-router docs https://github.com/ReactTraining/react-router/blob/master/packages/react-router-dom/docs/guides/scroll-restoration.md#scroll-to-top
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, { Component } from 'react' | |
import { withRouter } from 'react-router' | |
const topCoordinates = [0, 0] | |
const { scrollTo } = window | |
class ScrollToTopHandler extends Component { | |
// Arrow method, requires stage 2 Babel preset | |
render = () => null | |
componentDidUpdate (prevProps) { | |
if (this.props.location === prevProps.location) { | |
return | |
} | |
scrollTo(...topCoordinates) | |
} | |
} | |
export default withRouter(ScrollToTopHandler) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment