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