Created
June 16, 2021 09:17
-
-
Save ilxanlar/59f1207ae94477dfa3b3610e5ff1d082 to your computer and use it in GitHub Desktop.
This file contains 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, { useState, useEffect } from 'react' | |
function FriendStatus({ friendId }) { | |
const [isOnline, setIsOnline] = useState(null) | |
useEffect(() => { | |
function handleStatusChange(status) { | |
setIsOnline(status.isOnline) | |
} | |
ChatAPI.subscribeToFriendStatus(friendId, handleStatusChange) | |
return function cleanup() { | |
ChatAPI.unsubscribeFromFriendStatus(friendId, handleStatusChange) | |
} | |
}, [friendId]) // Pass [friendId] as dependencies | |
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