I'm trying to use useEffect to stop listening to Firebase Firestore collection changes.
I can get Firebase data, But I can't access this unsubscribe();
How can i fix this?
let unsbscribe;
async function getFirebaseData() {
unsbscribe = db.collection('room')
.where('joinUser', 'array-contains-any', [10])
.onSnapshot((res) => {
// do something...
});
useEffect(() => {
getFirebaseData();
return () => {
unsbscribe(); // throws an error that unsubscribe is not a function.
};
}, []);