Last active
June 16, 2021 09:13
-
-
Save ilxanlar/2765d013e3ecee152eba131401271817 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) | |
// Specify how to clean up after this effect: | |
return function cleanup() { | |
ChatAPI.unsubscribeFromFriendStatus(friendId, 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