Skip to content

Instantly share code, notes, and snippets.

@mngyuan
Created June 19, 2019 01:44
Show Gist options
  • Save mngyuan/40205b148716bb1d36ad9a2fab645644 to your computer and use it in GitHub Desktop.
Save mngyuan/40205b148716bb1d36ad9a2fab645644 to your computer and use it in GitHub Desktop.
Reset scroll on page transitions for wrapper components with React Router
.content-scrollable {
overflow: scroll;
height: 500px;
}
import React, {useEffect, useRef} from 'react';
import {withRouter} from 'react-router-dom';
const ScrollableArea = ({children, location: {pathname}, resetOnPage}) => {
const scrollableElem = useRef(null);
useEffect(
() => {
resetOnPage ? (scrollableElem.current.scrollTop = 0) : null;
},
[pathname],
);
return (
<div ref={scrollableElem} className="content-scrollable">
{children}
</div>
);
};
export default withRouter(ScrollableArea);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment