Skip to content

Instantly share code, notes, and snippets.

@marufsiddiqui
Created July 13, 2018 12:11
Show Gist options
  • Save marufsiddiqui/3c0d2e01e48de0a6a4ac721b227a834b to your computer and use it in GitHub Desktop.
Save marufsiddiqui/3c0d2e01e48de0a6a4ac721b227a834b to your computer and use it in GitHub Desktop.
RestoreScroll.js
class RestoreScroll extends React.Component {
rememberScroll () {
window.requestAnimationFrame(() => {
console.log('scrollY', window.scrollY)
window.sessionStorage.setItem(SCROLL_KEY, window.scrollY)
})
}
restoreScroll () {
const scrollPosition = window.sessionStorage.getItem(SCROLL_KEY)
console.log('scrollPosition', scrollPosition)
if (scrollPosition) {
setTimeout(() => {
window.scroll(0, scrollPosition)
}, 100)
}
}
componentDidMount () {
this.restoreScroll()
window.addEventListener('scroll', this.rememberScroll)
}
componentWillUnmount () {
window.removeEventListener('scroll', this.rememberScroll)
}
render () {
return this.props.children
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment