Created
March 4, 2021 14:58
-
-
Save xiaoyunyang/e0a710ea2ed9d3604ef9c40601fd3310 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, { 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