Skip to content

Instantly share code, notes, and snippets.

@hieptl
Last active October 15, 2021 12:34
Show Gist options
  • Save hieptl/6d31bc590a6b4ba212b66881ead1383e to your computer and use it in GitHub Desktop.
Save hieptl/6d31bc590a6b4ba212b66881ead1383e to your computer and use it in GitHub Desktop.
Chat.js - User Listener Event - React Native Gifted Chat App
...
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