Last active
October 15, 2021 12:34
-
-
Save hieptl/6d31bc590a6b4ba212b66881ead1383e to your computer and use it in GitHub Desktop.
Chat.js - User Listener Event - React Native Gifted Chat App
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
... | |
const Chat = (props) => { | |
... | |
const userOnlineListenerId = useRef(uuidv4()); | |
... | |
useEffect(() => { | |
if (selectedConversation) { | |
... | |
// listen for online users. | |
listenForOnlineUsers(); | |
} | |
return () => { | |
if (selectedConversation) { | |
... | |
cometChat.removeUserListener(userOnlineListenerId); | |
} | |
} | |
}, [selectedConversation]); | |
... | |
const listenForOnlineUsers = () => { | |
cometChat.addUserListener( | |
userOnlineListenerId, | |
new cometChat.UserListener({ | |
onUserOnline: onlineUser => { | |
if (onlineUser && onlineUser.uid === selectedConversation.uid) { | |
navigation.setOptions({ | |
headerTitle: () => renderChatHeaderTitle(onlineUser), | |
}) | |
} | |
}, | |
onUserOffline: offlineUser => { | |
if (offlineUser && offlineUser.uid === selectedConversation.uid) { | |
navigation.setOptions({ | |
headerTitle: () => renderChatHeaderTitle(offlineUser), | |
}) | |
} | |
} | |
}) | |
); | |
}; | |
... | |
}; | |
... |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment