Skip to content

Commit

Permalink
add hooks option to scroll restoration docs (#6762)
Browse files Browse the repository at this point in the history
  • Loading branch information
rnglnd authored and timdorr committed Jun 2, 2019
1 parent 0f5d701 commit 8ed894f
Showing 1 changed file with 14 additions and 0 deletions.
14 changes: 14 additions & 0 deletions packages/react-router-dom/docs/guides/scroll-restoration.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,20 @@ class ScrollToTop extends Component {
export default withRouter(ScrollToTop);
```

Or if you are running React 16.8 and above, you can use hooks:

```jsx
const ScrollToTop = ({ children, location: { pathname } }) => {
useEffect(() => {
window.scrollTo(0, 0);
}, [pathname]);

return children;
};

export default withRouter(ScrollToTop);
```

Then render it at the top of your app, but below Router

```jsx
Expand Down

0 comments on commit 8ed894f

Please sign in to comment.