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