Created
November 25, 2018 11:54
-
-
Save productioncoder/09d29194b97c8abaf585781450a3f19d to your computer and use it in GitHub Desktop.
Youtube in React: scrolling to top when URL changes
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 {withRouter} from 'react-router-dom'; | |
export class ScrollToTop extends React.Component { | |
componentDidUpdate(prevProps) { | |
if (this.props.location !== prevProps.location && window) { | |
window.scrollTo(0, 0); | |
} | |
} | |
render() { | |
return this.props.children; | |
} | |
} | |
export default withRouter(ScrollToTop); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment