Advertisement
Guest User

Untitled

a guest
Aug 22nd, 2019
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.77 KB | None | 0 0
  1. function useFriendStatus(friendID) {
  2. const [isOnline, setIsOnline] = useState(null);
  3.  
  4. useEffect(() => {
  5. function handleStatusChange(status) {
  6. setIsOnline(status.isOnline);
  7. }
  8.  
  9. ChatAPI.subscribeToFriendStatus(friendID, handleStatusChange);
  10. return () => {
  11. ChatAPI.unsubscribeFromFriendStatus(friendID, handleStatusChange);
  12. };
  13. });
  14.  
  15. return isOnline;
  16. }
  17.  
  18. function FriendStatus(props) {
  19. const isOnline = useFriendStatus(props.friend.id);
  20.  
  21. if (isOnline === null) {
  22. return 'Loading...';
  23. }
  24. return isOnline ? 'Online' : 'Offline';
  25. }
  26.  
  27. function FriendListItem(props) {
  28. const isOnline = useFriendStatus(props.friend.id);
  29.  
  30. return (
  31. <li style={{ color: isOnline ? 'green' : 'black' }}>
  32. {props.friend.name}
  33. </li>
  34. );
  35. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement