UNPKG

@hookit/network-status

Version:
24 lines 984 B
import * as React from 'react'; import { useWindowEventListener } from '@hookit/window-event-listener'; var Action; (function (Action) { Action["ONLINE"] = "online"; Action["OFFLINE"] = "offline"; })(Action || (Action = {})); const networkStatusReducer = (_, action) => { switch (action.type) { case Action.ONLINE: return true; case Action.OFFLINE: return false; } }; export const useNetworkStatus = () => { const [state, dispatch] = React.useReducer(networkStatusReducer, typeof window !== 'undefined' ? window.navigator.onLine : undefined); const onlineHandler = React.useCallback(() => dispatch({ type: Action.ONLINE }), [dispatch]); const offlineHandler = React.useCallback(() => dispatch({ type: Action.OFFLINE }), [dispatch]); useWindowEventListener(Action.ONLINE, onlineHandler); useWindowEventListener(Action.OFFLINE, offlineHandler); return state; }; //# sourceMappingURL=useNetworkStatus.js.map