使用Firebase实时数据库,有没有办法清理嵌套在侦听器.on()中的侦听器.on()?
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) ?**
}
}, []发布于 2021-11-01 14:49:35
管理这样的监听器并没有什么特别之处。您需要保留所附加的侦听器的列表,然后在效果过期时对它们调用off。
https://stackoverflow.com/questions/69793170
复制相似问题