Something went wrong. Try again.
[READ-ONLY] Mirror of https://github.com/wojtekmaj/react-hooks. A collection of React Hooks.
hooks react react-hooks
Something went wrong. Try again.
715 B · 27 lines
TypeScript
at main
12345678910111213141516171819202122232425262728import { useCallback, useState } from 'react';
import useEventListener from './useEventListener.js';
const isBrowser = typeof window !== 'undefined';
/** * Returns the online status of the browser. * * @returns {boolean | null} Online status of the browser */export default function useOnLine(): boolean | null { const [onLine, setOnLine] = useState(isBrowser ? navigator.onLine : null);
const handleOnline = useCallback(() => { setOnLine(true); }, []);
const handleOffline = useCallback(() => { setOnLine(false); }, []);
useEventListener(isBrowser ? window : null, 'online', handleOnline); useEventListener(isBrowser ? window : null, 'offline', handleOffline);
return onLine;}