Hello trying to make a conversations app based on twilio
The app is doing fine but everytime someone clicks a Conversation from the Conversations List, it starts a useEffect, the useEffect has something like the following function:
const setChannelEvents = useCallback((channel) => {
channel.on('messageAdded', (message) => {
setMessages(prevMessages => [...message, prevMessages])
})
The problem is that every time we leave the conversation that listener stays on so every time someone clicks on a conversation the app is creating a loot of listeners and well that isn't cool.
Wondering how to turn off the listener when it leaves the component
Tried something like this:
useEffect(() => {
Twilio.getClient()
.then((client) => client.getConversationBySid(channelId))
.then((channel) => setChannelEvents(channel))
.catch((err) => console.log('err', err.message))
.finally(() => setLoading(''))
return () => chatClientChannel.current.removeListener('messageAdded')
}, [channelId, setChannelEvents])
Didnt work
Any help would be appreciated, Thanks :)