I think I understand how to detach a listener by returning it, but in this specific case I'm not sure of the best way to do it.
I have a React component where I subscribe to auth changes with onAuthStateChanged.
If it returns an user : I create a listener to the data of this user in Firestore.
useEffect(() => {
return firebase.auth.onAuthStateChanged(user => {
if (user) {
const unsubscribeFirestore = firebase.db
.collection('users')
.doc(user.uid)
.onSnapshot(doc => {
setUser({ user, ...doc.data() })
})
} else {
setUser({ user })
}
})
}, [firebase])
Where and how should I return unsubscribeFirestore to detach it ?