Skip to content

React Hooks to detect when your browser is online/offline.

License

Notifications You must be signed in to change notification settings

cansin/use-navigator-online

Repository files navigation

use-navigator-online

tests codeql size dependencies downloads license

React Hooks to detect when your browser is online/offline using window.navigator.onLine.

Install

npm install use-navigator-online --save

Basic Usage

Update or create next.config.js with

import useNavigatorOnline from "use-navigator-online";

function Component() {
  const { isOnline, isOffline, backOnline, backOffline } = useNavigatorOnline();

  useEffect(() => {
    // Do something when network is back online.
  }, [backOnline]);

  useEffect(() => {
    // Do something when network goes offline.
  }, [backOffline]);

  return (
    <p>
      {isOnline ? "online" : "not online"}{" "}
      {isOffline ? "offline" : "not offline"}
    </p>
  );
}

Available Return Values

  • isOnline: boolean - true when online, false otherwise.
  • isOffline: boolean - true when offline, false otherwise.
  • backOnline: boolean - true when network status changes from offline to online, false otherwise.
  • backOffline: boolean - true when network status changes from online to offline, false otherwise.

About

React Hooks to detect when your browser is online/offline.

Topics

Resources

License

Security policy

Stars

Watchers

Forks

Packages

No packages published