首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >RTDB- How to cleanup .off() of listener .on complex ( listener .on in listener .on('child_add...) )

RTDB- How to cleanup .off() of listener .on complex ( listener .on in listener .on('child_add...) )
EN

Stack Overflow用户
提问于 2021-11-01 06:07:31
回答 1查看 24关注 0票数 0

使用Firebase实时数据库,有没有办法清理嵌套在侦听器.on()中的侦听器.on()?

代码语言:javascript
复制
useEffect(() => {

    const ChannelRef = database().ref(`/user/${uid}/channel`);
    ChannelRef.keepSynced(true);
    ChannelRef.on('child_added', channel_id => {
          var startTime = ....
          const threadRef = database().ref(`/channel/${channel_id.key}/Thread`).orderByKey().startAt(startTime);
          threadRef.keepSynced(true);
          threadRef.on('value', thread => {.     <--------------------------------   How to cleanup this listenner ?
            dispatch({
               type: 'AddThread',
               payload: thread.val()
            });
    })


    return () => {
          // CleanUp listenner
           ChannelRef.off();
           ChannelRef.keepSynced(false); 
         
          **// How to cleanup database().ref(`/channel/${channel_id.key}/Thread`).orderByKey().startAt(startTime) ?**
   }
  
}, []
EN

回答 1

Stack Overflow用户

发布于 2021-11-01 14:49:35

管理这样的监听器并没有什么特别之处。您需要保留所附加的侦听器的列表,然后在效果过期时对它们调用off

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/69793170

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档