Skip to main content
The onlineManager is a singleton that manages network connectivity state and notifies subscribers when the online state changes. It listens to the browser’s online and offline events by default and can be customized with custom event listeners.

Import

Methods

subscribe

Subscribe to online state changes.
(online: boolean) => void
required
Callback function that will be called when the online state changes. Receives a boolean indicating whether the network is online.
() => void
Returns an unsubscribe function that removes the listener when called.

Example

setEventListener

Set a custom event listener for online state changes. This allows you to customize how network connectivity is detected.
SetupFn
required
A setup function that receives a setOnline callback and returns an optional cleanup function.Type signature:
The setup function should call setOnline(true) when the network comes online and setOnline(false) when it goes offline.

Example

setOnline

Manually set the online state.
boolean
required
The new online state. Pass true to indicate the network is online, false to indicate it’s offline.

Example

isOnline

Get the current online state.
boolean
Returns true if the network is online, false otherwise.

Example

Default Behavior

By default, the onlineManager listens to the browser’s online and offline events to detect network connectivity changes. The manager starts with the assumption that the network is online (true). In environments where window.addEventListener is not available (like React Native), you need to set up a custom event listener using setEventListener.

Use Cases

Custom Online Detection

You can provide custom online detection logic for different environments:

Disable Automatic Online Refetching

You can prevent automatic refetching when the network reconnects:

Advanced Online Detection

You can implement more sophisticated online detection:

Testing

In tests, you can control the online state manually:

Responding to Network Changes

You can subscribe to network changes to update your UI or trigger custom logic: