removeEventListener()
works when I don't use throttle()
from lodash.
window.addEventListener('scroll', this.checkVisible, 1000, false);
window.removeEventListener('scroll', this.checkVisible, 1000, false);
(I bound the method in the constructor)
Unfortunately, with the throttle(this.checkVisible)
function wrapped around it - doesn't work. I assume it's because when trying to remove the listener, throttle() makes new instance and maybe I should bind it globally. How though (if that's the case)?
import React from 'react';
import throttle from 'lodash.throttle';
class About extends React.Component {
constructor(props) {
super(props);
this.checkVisible = this.checkVisible.bind(this);
}
componentDidMount() {
window.addEventListener('scroll', throttle(this.checkVisible, 1000), false);
}
checkVisible() {
if (window.scrollY > 450) {
// do something
window.removeEventListener('scroll', throttle(this.checkVisible, 1000),
false);
}
}
render() {
return (
<section id="about"> something
</section>
);
}
}
export default About;