Skip to content

Commit 41e2925

Browse files
committed
Add scroll to top on page change
1 parent 634aea5 commit 41e2925

File tree

2 files changed

+29
-4
lines changed

2 files changed

+29
-4
lines changed

Diff for: index.js

+7-4
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,18 @@
11
import React from 'react';
22
import ReactDOM from 'react-dom';
33
import { Provider } from 'react-redux';
4-
import { BrowserRouter } from 'react-router-dom';
4+
import { BrowserRouter as Router } from 'react-router-dom';
55
import store from './src/store';
66
import Layout from './src/Layout';
7+
import ScrollToTop from './src/components/ScrollToTop';
78

89
const App = () => (
910
<Provider store={store}>
10-
<BrowserRouter>
11-
<Layout />
12-
</BrowserRouter>
11+
<Router>
12+
<ScrollToTop>
13+
<Layout />
14+
</ScrollToTop>
15+
</Router>
1316
</Provider>
1417
);
1518

Diff for: src/components/ScrollToTop.js

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import React, { Component } from 'react';
2+
import PropTypes from 'prop-types';
3+
import { withRouter } from 'react-router-dom';
4+
5+
class ScrollToTop extends Component {
6+
componentDidUpdate(prevProps) {
7+
if (this.props.location.pathname !== prevProps.location.pathname) {
8+
window.scrollTo(0, 0);
9+
}
10+
}
11+
12+
render() {
13+
return this.props.children;
14+
}
15+
}
16+
17+
ScrollToTop.propTypes = {
18+
location: PropTypes.object.isRequired,
19+
children: PropTypes.object.isRequired
20+
};
21+
22+
export default withRouter(ScrollToTop);

0 commit comments

Comments
 (0)