Skip to content

Commit 267e217

Browse files
committed
scroll to top
1 parent 9a264a9 commit 267e217

File tree

2 files changed

+30
-9
lines changed

2 files changed

+30
-9
lines changed
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import React, { useEffect, Fragment } from 'react';
2+
import { withRouter } from 'react-router-dom';
3+
4+
function ScrollToTop({ history, children }) {
5+
useEffect(() => {
6+
const unlisten = history.listen(() => {
7+
window.scrollTo(0, 0);
8+
});
9+
return () => {
10+
unlisten();
11+
};
12+
}, []);
13+
14+
return <Fragment>{children}</Fragment>;
15+
}
16+
17+
export default withRouter(ScrollToTop);

reactjsfoundations.com/src/index.js

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
1-
import React from "react";
2-
import ReactDOM from "react-dom";
3-
import { BrowserRouter as Router } from "react-router-dom";
4-
import App from "./App";
1+
import React from 'react';
2+
import ReactDOM from 'react-dom';
3+
import { BrowserRouter as Router } from 'react-router-dom';
4+
import App from './App';
5+
import ScrollToTop from './ScrollToTop';
6+
57
ReactDOM.render(
6-
<Router>
7-
<App />
8-
</Router>,
9-
document.getElementById("root")
10-
);
8+
<Router>
9+
<ScrollToTop>
10+
<App />
11+
</ScrollToTop>
12+
</Router>,
13+
document.getElementById('root')
14+
);

0 commit comments

Comments
 (0)