Defined in: packages/query-core/src/focusManager.ts:14
The FocusManager manages the focus state within TanStack Query.
It can be used to change the default event listeners or to manually change the focus state.
hasListeners(): boolean;Defined in: packages/query-core/src/subscribable.ts:43
Returns true while at least one listener is registered, false once they have all unsubscribed.
boolean
true if at least one listener is registered.
Subscribable.hasListenersisFocused(): boolean;Defined in: packages/query-core/src/focusManager.ts:130
isFocused can be used to get the current focus state.
boolean
The focus state set with setFocused, or otherwise whether the document is visible.
onFocus(): void;Defined in: packages/query-core/src/focusManager.ts:119
onFocus notifies all subscribed listeners with the current focus state.
void
setEventListener(setup: SetupFn): void;Defined in: packages/query-core/src/focusManager.ts:78
setEventListener can be used to set a custom event listener that will be used to determine the focus state. The provided setup function receives a setFocused callback: call it with a boolean to manually set the focus state, or with no arguments to re-evaluate the current focus state and notify subscribers.
SetupFn
Receives the setFocused callback, registers the event listener, and may return a cleanup function that is called when the listener is replaced or no longer needed.
void
import { focusManager } from '@tanstack/query-core'
focusManager.setEventListener((handleFocus) => {
const listener = () => handleFocus()
// Listen to visibilitychange
if (typeof window !== 'undefined' && window.addEventListener) {
window.addEventListener('visibilitychange', listener, false)
}
return () => {
// Be sure to unsubscribe if a new handler is set
window.removeEventListener('visibilitychange', listener)
}
})setFocused(focused?: boolean): void;Defined in: packages/query-core/src/focusManager.ts:108
setFocused can be used to manually set the focus state. Set undefined to fall back to the default focus check.
boolean
The focus state, or undefined to use the default focus check.
void
import { focusManager } from '@tanstack/query-core'
// Set focused
focusManager.setFocused(true)
// Set unfocused
focusManager.setFocused(false)
// Fallback to the default focus check
focusManager.setFocused(undefined)subscribe(listener: Listener): () => void;Defined in: packages/query-core/src/subscribable.ts:28
Registers a listener to be called on every update this object notifies about. Returns a function that removes the listener again — call it to stop listening. The base class never drops a listener on its own, though some subclasses clear all of theirs in destroy().
Listener
Called on each update, with whatever the subclass passes to its subscribers.
A function that removes the listener.
(): void;void
const unsubscribe = subscribable.subscribe(() => {
// react to the update
})
unsubscribe()Subscribable.subscribe