Created
June 19, 2019 01:44
-
-
Save mngyuan/40205b148716bb1d36ad9a2fab645644 to your computer and use it in GitHub Desktop.
Reset scroll on page transitions for wrapper components with React Router
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
.content-scrollable { | |
overflow: scroll; | |
height: 500px; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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