Skip to content

Instantly share code, notes, and snippets.

@cheskoxd
Created March 8, 2024 23:35
Show Gist options
  • Save cheskoxd/c05e870540b2d2ab31f26408cd9a1ce9 to your computer and use it in GitHub Desktop.
Save cheskoxd/c05e870540b2d2ab31f26408cd9a1ce9 to your computer and use it in GitHub Desktop.
import { useEffect, useState } from "react";
export const useOnlineStatus = () => {
const [isOnline, setIsOnline] = useState(navigator.onLine);
const handleOnline = () => setIsOnline(true);
const handleOffline = () => setIsOnline(false);
useEffect(() => {
window.addEventListener("online", handleOnline);
window.addEventListener("offline", handleOffline);
return () => {
window.removeEventListener("online", handleOnline);
window.removeEventListener("offline", handleOffline);
};
}, []);
return isOnline;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment