Skip to content

useNetworkState

This hook can track the user connection changes and return whether is online or offline with some of the user network informations.

Return Values

NameTypeDescription
isOnlineBooleandetermines whether the user is online or not.
...informationsObjectthe NetworkInformation about the user connection

WARNING

Some of the NetworkInformation properties may not supported in other browsers. checkout Browser compatibility

Example Usage

tsx
import { useNetworkState } from 'react-pre-hooks';

export default function Example() {
  const { isOnline } = useNetworkState();

  return (
    <main>
      <p>
        You are <span>{isOnline ? 'online' : 'offline'}</span>.
      </p>
    </main>
  );
}
import { useNetworkState } from 'react-pre-hooks';

export default function Example() {
  const { isOnline } = useNetworkState();

  return (
    <main>
      <p>
        You are <span>{isOnline ? 'online' : 'offline'}</span>.
      </p>
    </main>
  );
}

Released under the MIT License.