Skip to content

Instantly share code, notes, and snippets.

@xiaoyunyang
Created March 4, 2021 14:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save xiaoyunyang/e0a710ea2ed9d3604ef9c40601fd3310 to your computer and use it in GitHub Desktop.
Save xiaoyunyang/e0a710ea2ed9d3604ef9c40601fd3310 to your computer and use it in GitHub Desktop.
import React, { useState } from "react";
function FriendStatus(props) {
const [isOnline, setIsOnline] = useState(null);
useEffect(() => {
function handleStatusChange(status) {
setIsOnline(status.isOnline);
}
ChatAPI.subscribeToFriendStatus(props.friend.id, handleStatusChange);
// Specify how to clean up after this effect:
return function cleanup() {
ChatAPI.unsubscribeFromFriendStatus(props.friend.id, handleStatusChange);
};
});
if (isOnline === null) {
return "Loading...";
}
return isOnline ? "Online" : "Offline";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment