Skip to content
File

Blob: src/client/hooks/use-online.ts

typescript19 lines
1import { useSyncExternalStore } from "react";
2 
3function subscribe(cb: () => void) {
4 window.addEventListener("online", cb);
5 window.addEventListener("offline", cb);
6 return () => {
7 window.removeEventListener("online", cb);
8 window.removeEventListener("offline", cb);
9 };
10}
11 
12function getSnapshot() {
13 return navigator.onLine;
14}
15 
16export function useOnline(): boolean {
17 return useSyncExternalStore(subscribe, getSnapshot);
18}