admin管理员组文章数量:1026989
I have a small react app. in One of my main ponents, I am adding an event listener on ponentDidMount but when I try to remove it on ponentWillUnmount it does not seem to be doing it. I have even tried putting them one after the other and still it does not seem to be removed.
Here is sample code of my situation (reduced from the real one)
listenerF(e){
console.log('im scrolling!'
//..actually does other stuff
}
ponentDidMount(){
window.addEventListener('scroll', e => this.listenerF(e))
}
ponentWillUnmount(){
window.removeEventListener('scroll',e=>this.listenerF(e))
console.log('unmounted')
}
I suspect it might be the arrow function acting as an anonymous function, and therefore removeEventListener isn't matching the functions properly.
I tried some alternatives to add the event listener with just a function call or without an arrow function, but it didnt seem to work like that, so Im not sure what may be wrong with my syntax or setup that the event is getting added right, but cannot remove it
I have a small react app. in One of my main ponents, I am adding an event listener on ponentDidMount but when I try to remove it on ponentWillUnmount it does not seem to be doing it. I have even tried putting them one after the other and still it does not seem to be removed.
Here is sample code of my situation (reduced from the real one)
listenerF(e){
console.log('im scrolling!'
//..actually does other stuff
}
ponentDidMount(){
window.addEventListener('scroll', e => this.listenerF(e))
}
ponentWillUnmount(){
window.removeEventListener('scroll',e=>this.listenerF(e))
console.log('unmounted')
}
I suspect it might be the arrow function acting as an anonymous function, and therefore removeEventListener isn't matching the functions properly.
I tried some alternatives to add the event listener with just a function call or without an arrow function, but it didnt seem to work like that, so Im not sure what may be wrong with my syntax or setup that the event is getting added right, but cannot remove it
Share Improve this question asked Oct 29, 2018 at 4:26 xunuxxunux 1,7795 gold badges22 silver badges42 bronze badges 1-
You don't need the arrow function, do this.functionName directly, you will still be able to access your event argument without issues, you'll probably also need to bind your handler
this.functionName.bind(this)
– nedo Commented Oct 29, 2018 at 4:31
2 Answers
Reset to default 6React injects the event to your handler automatically there is no need to wrap it in an arrow function just to pass the event, the problem is that React had no refference to the function that you passed to your handler.
listenerF(e) { // dont forget to add the event as a parameter here
console.log(e);
console.log("im scrolling!");
}
ponentDidMount() {
window.addEventListener("scroll", this.listenerF);
}
ponentWillUnmount() {
window.removeEventListener("scroll", this.listenerF);
console.log("unmounted");
}
The problem here is that you are using two different functions: One that is created when you add the event listener (the arrow function). And a different one that you use when you try to remove the listener (the arrow function.)
As the documentation state:
The event listener to be removed is identified using a bination of the event type, the event listener function itself, and various optional options that may affect the matching process.
https://developer.mozilla/en-US/docs/Web/API/EventTarget/removeEventListener
I have a small react app. in One of my main ponents, I am adding an event listener on ponentDidMount but when I try to remove it on ponentWillUnmount it does not seem to be doing it. I have even tried putting them one after the other and still it does not seem to be removed.
Here is sample code of my situation (reduced from the real one)
listenerF(e){
console.log('im scrolling!'
//..actually does other stuff
}
ponentDidMount(){
window.addEventListener('scroll', e => this.listenerF(e))
}
ponentWillUnmount(){
window.removeEventListener('scroll',e=>this.listenerF(e))
console.log('unmounted')
}
I suspect it might be the arrow function acting as an anonymous function, and therefore removeEventListener isn't matching the functions properly.
I tried some alternatives to add the event listener with just a function call or without an arrow function, but it didnt seem to work like that, so Im not sure what may be wrong with my syntax or setup that the event is getting added right, but cannot remove it
I have a small react app. in One of my main ponents, I am adding an event listener on ponentDidMount but when I try to remove it on ponentWillUnmount it does not seem to be doing it. I have even tried putting them one after the other and still it does not seem to be removed.
Here is sample code of my situation (reduced from the real one)
listenerF(e){
console.log('im scrolling!'
//..actually does other stuff
}
ponentDidMount(){
window.addEventListener('scroll', e => this.listenerF(e))
}
ponentWillUnmount(){
window.removeEventListener('scroll',e=>this.listenerF(e))
console.log('unmounted')
}
I suspect it might be the arrow function acting as an anonymous function, and therefore removeEventListener isn't matching the functions properly.
I tried some alternatives to add the event listener with just a function call or without an arrow function, but it didnt seem to work like that, so Im not sure what may be wrong with my syntax or setup that the event is getting added right, but cannot remove it
Share Improve this question asked Oct 29, 2018 at 4:26 xunuxxunux 1,7795 gold badges22 silver badges42 bronze badges 1-
You don't need the arrow function, do this.functionName directly, you will still be able to access your event argument without issues, you'll probably also need to bind your handler
this.functionName.bind(this)
– nedo Commented Oct 29, 2018 at 4:31
2 Answers
Reset to default 6React injects the event to your handler automatically there is no need to wrap it in an arrow function just to pass the event, the problem is that React had no refference to the function that you passed to your handler.
listenerF(e) { // dont forget to add the event as a parameter here
console.log(e);
console.log("im scrolling!");
}
ponentDidMount() {
window.addEventListener("scroll", this.listenerF);
}
ponentWillUnmount() {
window.removeEventListener("scroll", this.listenerF);
console.log("unmounted");
}
The problem here is that you are using two different functions: One that is created when you add the event listener (the arrow function). And a different one that you use when you try to remove the listener (the arrow function.)
As the documentation state:
The event listener to be removed is identified using a bination of the event type, the event listener function itself, and various optional options that may affect the matching process.
https://developer.mozilla/en-US/docs/Web/API/EventTarget/removeEventListener
本文标签: javascriptReactJSremoving event listener on componentWillUnmount Stack Overflow
版权声明:本文标题:javascript - Reactjs, removing event listener on componentWillUnmount, - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745277804a2143198.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论