Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@productioncoder
Created November 25, 2018 11:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save productioncoder/09d29194b97c8abaf585781450a3f19d to your computer and use it in GitHub Desktop.
Save productioncoder/09d29194b97c8abaf585781450a3f19d to your computer and use it in GitHub Desktop.
Youtube in React: scrolling to top when URL changes
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