Home > OS >  How to detach firebase Realtime listeners?
How to detach firebase Realtime listeners?

Time:11-04

I'm using the firebase real-time database version 9, and I use this method to retrieve back data:

const refList= ref(
  db,
  `users/${uid}`
);
onValue(refList, (snapshot) => {
  let allData = snapshot.val() === null ? [] : snapshot.val()
});

So this is real-time communication therefore this ref has to use a listener. I checked their documentation, they say to use the off() method onto the ref without any code examples.

refList.off()

Will this remove the listener?

CodePudding user response:

The onValue call now returns a function that (when called) removes the listener. So:

const unsubscribe = onValue(refList, (snapshot) => {
  let allData = snapshot.val() === null ? [] : snapshot.val()
});

And then later:

unsubscribe();
  • Related