Open History to see the last commit
1---
2id: OnlineManager
3title: OnlineManager
4redirect_from:
5 - reference/onlineManager
6 - framework/react/reference/onlineManager
7---
8β
9Defined in: [packages/query-core/src/onlineManager.ts:15](https://github.com/TanStack/query/blob/main/packages/query-core/src/onlineManager.ts#L15)
10β
11The `OnlineManager` manages the online state within TanStack Query. It can
12be used to change the default event listeners or to manually change the
13online state.
14β
15By default, the `onlineManager` assumes an active network connection, and
16listens to the `online` and `offline` events on the `window` object to
17detect changes.
18β
19## Extends
20β
21- `Subscribable`\<`Listener`\>
22β
23## Methods
24β
25### hasListeners()
26β
27```ts
28hasListeners(): boolean;
29```
30β
31Defined in: [packages/query-core/src/subscribable.ts:43](https://github.com/TanStack/query/blob/main/packages/query-core/src/subscribable.ts#L43)
32β
33Returns `true` while at least one listener is registered, `false` once they have all unsubscribed.
34β
35#### Returns
36β
37`boolean`
38β
39`true` if at least one listener is registered.
40β
41#### Inherited from
42β
43```ts
44Subscribable.hasListeners
45```
46β
47***
48β
49### isOnline()
50β
51```ts
52isOnline(): boolean;
53```
54β
55Defined in: [packages/query-core/src/onlineManager.ts:111](https://github.com/TanStack/query/blob/main/packages/query-core/src/onlineManager.ts#L111)
56β
57`isOnline` can be used to get the current online state.
58β
59#### Returns
60β
61`boolean`
62β
63`true` if online.
64β
65***
66β
67### setEventListener()
68β
69```ts
70setEventListener(setup: SetupFn): void;
71```
72β
73Defined in: [packages/query-core/src/onlineManager.ts:76](https://github.com/TanStack/query/blob/main/packages/query-core/src/onlineManager.ts#L76)
74β
75`setEventListener` can be used to set a custom event listener that will
76be used to determine the online state. The provided `setup` function
77receives a `setOnline` callback that should be called with a `boolean`
78whenever the online state changes.
79β
80#### Parameters
81β
82##### setup
83β
84`SetupFn`
85β
86Receives the `setOnline` callback, registers the event listener, and may return
87a cleanup function that is called when the listener is replaced or no longer needed.
88β
89#### Returns
90β
91`void`
92β
93#### Example
94β
95```ts
96import NetInfo from '@react-native-community/netinfo'
97import { onlineManager } from '@tanstack/query-core'
98β
99onlineManager.setEventListener((setOnline) => {
100 return NetInfo.addEventListener((state) => {
101 setOnline(!!state.isConnected)
102 })
103})
104```
105β
106***
107β
108### setOnline()
109β
110```ts
111setOnline(online: boolean): void;
112```
113β
114Defined in: [packages/query-core/src/onlineManager.ts:96](https://github.com/TanStack/query/blob/main/packages/query-core/src/onlineManager.ts#L96)
115β
116`setOnline` can be used to manually set the online state.
117β
118#### Parameters
119β
120##### online
121β
122`boolean`
123β
124The online state.
125β
126#### Returns
127β
128`void`
129β
130#### Example
131β
132```ts
133import { onlineManager } from '@tanstack/query-core'
134β
135// Set to online
136onlineManager.setOnline(true)
137β
138// Set to offline
139onlineManager.setOnline(false)
140```
141β
142***
143β
144### subscribe()
145β
146```ts
147subscribe(listener: Listener): () => void;
148```
149β
150Defined in: [packages/query-core/src/subscribable.ts:28](https://github.com/TanStack/query/blob/main/packages/query-core/src/subscribable.ts#L28)
151β
152Registers a listener to be called on every update this object notifies about. Returns a function
153that removes the listener again β call it to stop listening. The base class never drops a listener
154on its own, though some subclasses clear all of theirs in `destroy()`.
155β
156#### Parameters
157β
158##### listener
159β
160`Listener`
161β
162Called on each update, with whatever the subclass passes to its subscribers.
163β
164#### Returns
165β
166A function that removes the listener.
167β
168() => `void`
169β
170#### Example
171β
172```ts
173const unsubscribe = subscribable.subscribe(() => {
174 // react to the update
175})
176β
177unsubscribe()
178```
179β
180#### Inherited from
181β
182```ts
183Subscribable.subscribe
184```