Default browser connectivity
A browser application needs TanStack Query to react to normal connectivity changes.
TanStack · Const
onlineManager is TanStack Query's singleton controller for application connectivity. It reads browser online/offline events by default and supports custom listeners or manual state changes.
The singleton tracks whether TanStack Query considers the application connected. It normally derives this from browser online and offline events, but you can replace that detection with a custom listener or directly override the state.
const onlineManager: OnlineManager;| Name | Type | Description |
|---|---|---|
online | boolean | undefined | The boolean connectivity value supplied to setOnline; the API also supports undefined for the default check. |
setup | (setOnline: (online: boolean) => void) => (() => void) | A setup function that receives setOnline and returns listener cleanup. |
Singleton OnlineManager instance; isOnline returns boolean and listener-registration status returns boolean.
import { onlineManager } from '@tanstack/react-query'
onlineManager.setOnline(false)
console.log(onlineManager.isOnline())
onlineManager.setOnline(true)Manually toggles TanStack Query's connectivity state and reads it back.
A browser application needs TanStack Query to react to normal connectivity changes.
A React Native or other non-browser runtime provides its own network-status subscription.
Tests or application infrastructure need to force online or offline behavior.
onlineManager is the singleton OnlineManager used to manage and observe TanStack Query's online state. It exposes methods for querying the state, overriding it, and installing custom detection.
Follow-up: How would you support React Native connectivity? Install a platform network listener and pass its connection status to the callback received by setEventListener.
Call onlineManager.isOnline() to read the current status. It returns a boolean, with true meaning TanStack Query currently considers the app online.
Follow-up: What should code do when the status changes? Use setOnline through the configured listener so TanStack Query receives each new boolean state.
Use onlineManager.setEventListener to replace the default browser event handling. Its setup function receives setOnline, and the setup function should return cleanup for the installed listener.
Follow-up: When is direct setOnline useful? It is useful when application code already knows connectivity or must manually force the state during testing or specialized runtime integration.
OnlineManagerisOnlinesetEventListenersetOnline