For receiving data from Firebase Realtime Database I need to register listener:
objectReference.addValueEventListener(valueEventListener);
You can also do it like this:
componentWillUnmount() {
firebase.database().ref('example').child(this.state.somethingDyamic).off('value');
};
doSomething() {
firebase.database().ref('example').child(this.state.somethingDyamic).on('value', (snapshot) => {
...
});
}
The correct way to remove a listener is to remove it accordingly to the life-cycle of your activity using this line of code:
databaseReference.removeEventListener(valueEventListener);
Note that, if you have added the listener in onStart you have to remove it in onStop. If you have added the listener in onResume you have to remove it in onPause. If you have added the listener in onCreate you have to remove it in onDestroy.
But remember onDestroy is not always called.
Its better to check whether listener is null or has an object, because if the listener object is null there will be a runtime error
if(valueEventListener!=null){
databaseReference.removeEventListener(valueEventListener);
}