1 import { useEffect, useState } from 'react';
3 export const getOnlineStatus = () => {
4 return typeof navigator !== 'undefined' && typeof navigator.onLine === 'boolean' ? navigator.onLine : true;
7 const useOnline = () => {
8 const [onlineStatus, setOnlineStatus] = useState(getOnlineStatus());
11 const handleOnlineStatus = () => {
12 setOnlineStatus(getOnlineStatus());
15 window.addEventListener('online', handleOnlineStatus);
16 window.addEventListener('offline', handleOnlineStatus);
18 window.removeEventListener('online', handleOnlineStatus);
19 window.removeEventListener('offline', handleOnlineStatus);
26 export default useOnline;