feat: init
This commit is contained in:
22
apps/web/core/hooks/use-online-status.ts
Normal file
22
apps/web/core/hooks/use-online-status.ts
Normal file
@@ -0,0 +1,22 @@
|
||||
import { useState, useEffect } from "react";
|
||||
|
||||
const useOnlineStatus = () => {
|
||||
// states
|
||||
const [isOnline, setIsOnline] = useState(typeof navigator !== "undefined" ? navigator.onLine : true);
|
||||
|
||||
const updateOnlineStatus = () => setIsOnline(navigator.onLine);
|
||||
|
||||
useEffect(() => {
|
||||
window.addEventListener("online", updateOnlineStatus);
|
||||
window.addEventListener("offline", updateOnlineStatus);
|
||||
|
||||
return () => {
|
||||
window.removeEventListener("online", updateOnlineStatus);
|
||||
window.removeEventListener("offline", updateOnlineStatus);
|
||||
};
|
||||
}, []);
|
||||
|
||||
return { isOnline };
|
||||
};
|
||||
|
||||
export default useOnlineStatus;
|
||||
Reference in New Issue
Block a user