On previous versions, I would do:
// Declaring db reference
let ref = firebase.database().ref('features')
// Creating the listener
let listener = ref.on('value', snapshot => {
if(snapshot.val()){
// Reading data
}
}
// Unsubscribing
ref.off('value', listener)
After Firebase 9.0.0, I've seen that the onValue()
function returns an Unsubscribe callback:
/** A callback that can invoked to remove a listener. */
export declare type Unsubscribe = () => void;
Thus, my current approach:
// Declaring db reference
let featuresRef = ref(db, 'features')
// Creating the listener
let unsubscribe = onValue(featuresRef, snapshot => {
if(snapshot.val()){
// Reading data
}
})
// Unsubscribing
unsubscribe()
I see on the functions definition that the off()
function still exists, and per the documentation:
Callbacks are removed by calling the off() method on your Firebase database reference.
Do I need to use the returned Unsubscribe callback function or the off()
function to remove the listener?