admin管理员组文章数量:1201648
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;
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;
Share
Improve this question
edited Feb 14, 2021 at 15:11
Brian Tompsett - 汤莱恩
5,88372 gold badges61 silver badges133 bronze badges
asked Oct 29, 2017 at 16:22
SebastianSebastian
1,3051 gold badge17 silver badges27 bronze badges
1 Answer
Reset to default 25Lodash trottle creates a throttled function so you need to store a reference to it in order to remove the eventlistener.
import React from 'react';
import throttle from 'lodash.throttle';
class About extends React.Component {
constructor(props) {
super(props);
this.checkVisible = this.checkVisible.bind(this);
// Store a reference to the throttled function
this.trottledFunction = throttle(this.checkVisible, 1000);
}
componentDidMount() {
// Use reference to function created by lodash throttle
window.addEventListener('scroll', this.trottledFunction, false);
}
checkVisible() {
if (window.scrollY > 450) {
// do something
window.removeEventListener('scroll', this.trottledFunction, false);
}
}
render() {
return (
<section id="about"> something
</section>
);
}
}
export default About;
本文标签: javascriptRemoving event listener in React (lodashthrottle)Stack Overflow
版权声明:本文标题:javascript - Removing event listener in React (lodash.throttle) - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1738605072a2102295.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论