I\'m building an app (ES6) and I\'m curious what is the \'correct\' way to catch scroll up / down events?
I tried (npm) installing react-scroll-listener but I couldn\'t
This is general advice for hooking into any listeners:
Attach stuff in componentDidMount
, unattach in componentWillUnmount
class Whatever extends Component {
componentDidMount() {
window.addEventListener('scroll', this.handleScroll, { passive: true })
}
componentWillUnmount() {
window.removeEventListener('scroll', this.handleScroll)
}
handleScroll(event) {
// do something like call `this.setState`
// access window.scrollY etc
}
}