UNPKG

react-network-status-ts

Version:

React hook and component to detect and display online/offline network status.

16 lines (15 loc) 597 B
import { useEffect, useState } from "react"; export const useNetworkStatus = () => { const [isOnline, setIsOnline] = useState(navigator.onLine); useEffect(() => { const handleOnline = () => setIsOnline(true); const handleOffline = () => setIsOnline(false); window.addEventListener("online", handleOnline); window.addEventListener("offline", handleOffline); return () => { window.removeEventListener("online", handleOnline); window.removeEventListener("offline", handleOffline); }; }, []); return isOnline; };