Skip to content

Commit 1de3e6f

Browse files
committed
Scroll to top on routes changes done
* simple fix for v4 router * need to integrate lint and clean
1 parent 23835a0 commit 1de3e6f

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-1
lines changed

src/components/tools/ScrollToTop.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/* eslint-disable no-undef */
2+
import { Component } from 'react';
3+
import { withRouter } from 'react-router-dom';
4+
5+
class ScrollToTop extends Component {
6+
componentDidUpdate(prevProps) {
7+
const { location } = this.props;
8+
if (location.pathname !== prevProps.location.pathname) {
9+
window.scrollTo(0, 0);
10+
}
11+
}
12+
13+
render() {
14+
const { children } = this.props;
15+
return children;
16+
}
17+
}
18+
19+
export default withRouter(ScrollToTop);

src/index.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import React from 'react';
22
import ReactDOM from 'react-dom';
33
import { BrowserRouter } from 'react-router-dom';
4+
import ScrollToTop from './components/tools/ScrollToTop';
45

56
import './index.css';
67
import App from './App';
@@ -9,7 +10,9 @@ import 'bootstrap/dist/css/bootstrap.min.css';
910

1011
const app = (
1112
<BrowserRouter>
12-
<App />
13+
<ScrollToTop>
14+
<App />
15+
</ScrollToTop>
1316
</BrowserRouter>
1417
);
1518

0 commit comments

Comments
 (0)