Skip to main content

useIsOnline

A React hook that tracks the browser's online/offline status and automatically updates when the connection state changes.

React 18.0.0
npm install @plenty-hooks/use-is-online

Showcase

import useIsOnline from '@plenty-hooks/use-is-online';

function ConnectionStatus() {
const isOnline = useIsOnline();

return (
<div>
{isOnline ? (
<div>You are online</div>
) : (
<div>You are offline. Please check your connection.</div>
)}
</div>
);
}

API

useIsOnline()

Returns the current online status of the browser and automatically subscribes to online/offline events.

Returns

  • boolean - true if the browser is online, false if offline

Server-Side Rendering (SSR)

During server-side rendering the hook returns true.