Javascript 这是如何在react中取消订阅活动的?

Javascript 这是如何在react中取消订阅活动的?,javascript,reactjs,Javascript,Reactjs,我想在组件被销毁时取消订阅事件 我不确定我这样做是否正确 异步组件didmount(){ 常量索赔编号='T1339838' 常数{ 运行时 }=this.context const data=wait runtime.select('comments-get'{ 索赔编号:索赔编号 }) 这是我的国家({ 评论数据:数据 }) const commentsUpdated=runtime.fb.ref(“/comments”) commentsUpdated.on('value',childSn

我想在组件被销毁时取消订阅事件

我不确定我这样做是否正确

异步组件didmount(){ 常量索赔编号='T1339838' 常数{ 运行时 }=this.context const data=wait runtime.select('comments-get'{ 索赔编号:索赔编号 }) 这是我的国家({ 评论数据:数据 }) const commentsUpdated=runtime.fb.ref(“/comments”) commentsUpdated.on('value',childSnapshot=>{ this.updateComments(childSnapshot.val()) }) } 组件将卸载(){ //TODO:在侦听器上删除 window.removeEventListener('resize',this.commentsUpdated) }
window.removeEventListener仅当事件侦听器添加到窗口对象时才应使用。例如,如果要使用以下代码添加事件侦听器:

//添加一个侦听器

window.addEventListener('resize', this.commentsUpdated);
然后,在componentWillUnmount中,必须使用以下代码删除事件侦听器:

componentWillUnmount() {
    // TODO: Remove on listener
    window.removeEventListener('resize', this.commentsUpdated)
}

在上述情况下,只需清空或为this.commentsUpdated分配null即可。

组件将卸载
是取消订阅事件/订阅的正确位置-我不太明白为什么您要尝试从
调整大小
事件中删除与注释相关的处理程序!