Files
hats-app/hooks/useOnlineStatus.tsx
T
2025-12-19 13:57:55 +07:00

20 lines
548 B
TypeScript

import { useSyncExternalStore } from "react";
export default function useOnlineStatus(){
const isOnline = useSyncExternalStore(subscribe, getSnapshot)
return isOnline
function getSnapshot(){
return navigator.onLine
}
function subscribe(callback: any){
window.addEventListener('online', callback)
window.addEventListener('offline', callback)
return () => {
window.removeEventListener('online', callback)
window.removeEventListener('offline', callback)
}
}
}