Skip to content

Commit 8ed894f

Browse files
rnglndtimdorr
authored andcommittedJun 2, 2019
add hooks option to scroll restoration docs (#6762)
1 parent 0f5d701 commit 8ed894f

File tree

1 file changed

+14
-0
lines changed

1 file changed

+14
-0
lines changed
 

‎packages/react-router-dom/docs/guides/scroll-restoration.md

+14
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,20 @@ class ScrollToTop extends Component {
2626
export default withRouter(ScrollToTop);
2727
```
2828

29+
Or if you are running React 16.8 and above, you can use hooks:
30+
31+
```jsx
32+
const ScrollToTop = ({ children, location: { pathname } }) => {
33+
useEffect(() => {
34+
window.scrollTo(0, 0);
35+
}, [pathname]);
36+
37+
return children;
38+
};
39+
40+
export default withRouter(ScrollToTop);
41+
```
42+
2943
Then render it at the top of your app, but below Router
3044

3145
```jsx

0 commit comments

Comments
 (0)
Please sign in to comment.