Last active
March 23, 2020 17:04
-
-
Save ryardley/ba4c31250b89fc731210d3a31d4d14fc to your computer and use it in GitHub Desktop.
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
import React from 'react'; | |
import useFriendStatus from './useFriendStatus'; | |
export default function FriendListItem(props) { | |
const isOnline = useFriendStatus(props.friend.id); | |
return ( | |
<li style={{ color: isOnline ? 'green' : 'black' }}> | |
{props.friend.name} | |
</li> | |
); | |
} |
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
import { useState, useEffect } from 'react'; | |
export function useFriendStatus(friendID) { | |
const [isOnline, setIsOnline] = useState(null); | |
function handleStatusChange(status) { | |
setIsOnline(status.isOnline); | |
} | |
useEffect(() => { | |
ChatAPI.subscribeToFriendStatus(friendID, handleStatusChange); | |
return () => { | |
ChatAPI.unsubscribeFromFriendStatus(friendID, handleStatusChange); | |
}; | |
}, [handleStatusChange, ChatAPI]); | |
return isOnline; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Export function useFriendStatus?