Skip to content

Instantly share code, notes, and snippets.

@ilxanlar
Last active June 16, 2021 09:13
Show Gist options
  • Save ilxanlar/2765d013e3ecee152eba131401271817 to your computer and use it in GitHub Desktop.
Save ilxanlar/2765d013e3ecee152eba131401271817 to your computer and use it in GitHub Desktop.
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