Skip to content

Commit

Permalink
scroll to top on route change
Browse files Browse the repository at this point in the history
  • Loading branch information
sampl committed Sep 27, 2018
1 parent 6467738 commit af7b205
Showing 1 changed file with 14 additions and 0 deletions.
14 changes: 14 additions & 0 deletions src/views/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ const App = () => (
<BrowserRouter>
<ErrorBoundary>
<Layout>
<Route path="/" component={ScrollToTop}/>
<Route path="/" component={Analytics}/>
<Routes />
</Layout>
Expand All @@ -23,6 +24,19 @@ const App = () => (
</FirestoreProvider>
)

// scroll to top on route change
// https://github.com/ReactTraining/react-router/blob/master/packages/react-router-dom/docs/guides/scroll-restoration.md#scroll-to-top
class ScrollToTop extends React.Component {
componentDidUpdate(prevProps) {
if (this.props.location !== prevProps.location) {
window.scrollTo(0, 0)
}
}
render() {
return null
}
}

// Track Google Analytics page view for every route
// https://github.com/react-ga/react-ga/issues/122#issuecomment-319546248
const Analytics = ({location}) => {
Expand Down

0 comments on commit af7b205

Please sign in to comment.