Created
July 13, 2018 12:11
-
-
Save marufsiddiqui/3c0d2e01e48de0a6a4ac721b227a834b to your computer and use it in GitHub Desktop.
RestoreScroll.js
This file contains hidden or 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
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