RFC: Internet connectivity sensor
The below snippet detects changes of the online status of a browser. This should be embedded and provided as a feature from the Coko client.
- https://developer.mozilla.org/en-US/docs/Web/API/Navigator/onLine
- https://developer.mozilla.org/en-US/docs/Web/API/Window/online_event
- https://developer.mozilla.org/en-US/docs/Web/API/Window/offline_event
const [isOnline, setIsOnline] = useState(navigator.onLine)
useEffect(() => {
// Update network status
const handleStatusChange = () => {
setIsOnline(navigator.onLine)
}
// Listen to the online status
window.addEventListener('online', handleStatusChange)
// Listen to the offline status
window.addEventListener('offline', handleStatusChange)
// Specify how to clean up after this effect for performance improvement
return () => {
window.removeEventListener('online', handleStatusChange)
window.removeEventListener('offline', handleStatusChange)
}
}, [isOnline])