Spaces:
Sleeping
Sleeping
Download frontend/src/hooks/useNetworkStatus.js from ankitGhadiya/FarmerHelp: direct link, hf CLI and curl.
- Browser
- Download file 977 Bytes
-
https://huggingface.co/spaces/ankitGhadiya/FarmerHelp/resolve/main/frontend/src/hooks/useNetworkStatus.js
- Command line
-
hf download hf://spaces/ankitGhadiya/FarmerHelp/frontend/src/hooks/useNetworkStatus.js
-
curl -L -o useNetworkStatus.js https://huggingface.co/spaces/ankitGhadiya/FarmerHelp/resolve/main/frontend/src/hooks/useNetworkStatus.js
977 Bytes
| /** | |
| * useNetworkStatus - Hook for detecting online/offline connectivity. | |
| * | |
| * Returns a boolean indicating whether the browser currently has | |
| * network connectivity. Listens to the "online" and "offline" events | |
| * on the window object and re-renders when the status changes. | |
| * | |
| * @returns {boolean} True if online, false if offline. | |
| */ | |
| import { useState, useEffect } from "react"; | |
| export default function useNetworkStatus() { | |
| const [isOnline, setIsOnline] = useState( | |
| typeof navigator !== "undefined" ? navigator.onLine : true, | |
| ); | |
| useEffect(() => { | |
| function handleOnline() { | |
| setIsOnline(true); | |
| } | |
| function handleOffline() { | |
| setIsOnline(false); | |
| } | |
| window.addEventListener("online", handleOnline); | |
| window.addEventListener("offline", handleOffline); | |
| return () => { | |
| window.removeEventListener("online", handleOnline); | |
| window.removeEventListener("offline", handleOffline); | |
| }; | |
| }, []); | |
| return isOnline; | |
| } | |