2022-11-19 10:33:12 -08:00
|
|
|
import { useEffect, useState } from 'react'
|
|
|
|
|
|
|
|
const useOnlineStatus = () => {
|
2023-03-05 12:33:57 -08:00
|
|
|
const [online, isOnline] = useState(navigator.onLine)
|
2022-11-19 10:33:12 -08:00
|
|
|
|
|
|
|
const setOnline = () => {
|
|
|
|
isOnline(true)
|
|
|
|
}
|
|
|
|
const setOffline = () => {
|
|
|
|
isOnline(false)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Register the event listeners
|
|
|
|
useEffect(() => {
|
|
|
|
window.addEventListener('offline', setOffline)
|
|
|
|
window.addEventListener('online', setOnline)
|
|
|
|
|
|
|
|
// cleanup if we unmount
|
|
|
|
return () => {
|
|
|
|
window.removeEventListener('offline', setOffline)
|
|
|
|
window.removeEventListener('online', setOnline)
|
|
|
|
}
|
|
|
|
}, [])
|
|
|
|
|
|
|
|
return online
|
|
|
|
}
|
|
|
|
|
|
|
|
export default useOnlineStatus
|