I'm building a chat app with firebase firestore database and reactjs
I'm trying to use react hooks to manage the data flow but am having trouble detaching the firestore listener.
This use effect is where I am initialising the listener.
useEffect(() => {
setMessages(null)
roomId && listenForMessages(roomId, setMessages, false, scrollToBottom)
return () => {
if (active && setMessages) {
//Detach here
listenForMessages(roomId, setMessages, true)
}
}
}, [roomId ])
This is my listener function
export const listenForMessages = (roomId, setMessages, detach, scrollToBottom) => {
let listener = db
.collection('chats')
.doc(roomId)
.collection('messages')
.orderBy('timestamp')
.onSnapshot(
docSnapshot => {
let data = []
docSnapshot.docs.forEach(doc => data.push({ id: doc.id, ...doc.data() }))
setMessages(data)
scrollToBottom()
},
err => {
console.log(`Encountered error: ${err}`)
}
)
if (detach === true) {
listener()
}
}
This dosnt seem to detach previously attached listeners. As if I open chat room A then navigate to chat room B and chat room A gets a message, it will call over writting messages.