mirror of
https://github.com/ichsankurnia/hats-app.git
synced 2026-12-05 10:46:29 +00:00
20 lines
548 B
TypeScript
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)
|
|
}
|
|
}
|
|
} |