Connect custom focus events
An app uses a platform lifecycle or custom event source instead of browser focus events.
TanStack · Const
focusManager is TanStack Query's singleton focus-state controller. It lets you customize focus events, manually override focus, and inspect the current state.
The singleton focusManager controls the focus state observed by TanStack Query. Use setFocused for an explicit state, setFocused(undefined) to return to the default document-visibility check, or setEventListener to replace how focus changes are detected.
const focusManager: FocusManager;| Name | Type | Description |
|---|---|---|
setup | (setFocused: (focused?: boolean) => void) => () => void | Callback that installs custom focus detection and returns cleanup for the installed listener. |
focused | boolean | undefined | Explicit focus value; undefined restores the default focus check. |
focusManager is a singleton FocusManager instance; isFocused() returns the current focus state.
import { focusManager } from '@tanstack/react-query'
focusManager.setFocused(false)
// Later, restore the normal document-visibility check
focusManager.setFocused(undefined)
console.log(focusManager.isFocused())Temporarily overrides TanStack Query's focus state, then restores the default check and reads the current result.
An app uses a platform lifecycle or custom event source instead of browser focus events.
A workflow must temporarily prevent focus-driven query activity.
Code needs to inspect the focus state currently seen by TanStack Query.
focusManager is the singleton FocusManager instance used to manage and observe focus state in TanStack Query. It exposes methods for custom event handling, manual state changes, and reading the current state.
Follow-up: How would you integrate a non-browser lifecycle? Call setFocused with true or false from that lifecycle, and pass undefined when control should return to the default check.
setFocused manually sets the focus state. Passing true or false forces the corresponding state, while passing undefined restores the default focus check.
Follow-up: Why might setFocused(undefined) be necessary after a temporary override? Without it, the manual state remains authoritative instead of allowing the normal document-visibility check to determine focus.
setEventListener accepts setup code that receives a callback for reporting focus changes. The setup should register the callback with the desired events and return an unsubscribe function for cleanup.
Follow-up: What should happen when a second custom listener is installed? The first listener's cleanup must run so its event handlers do not remain active.
FocusManagersetEventListenersetFocusedisFocused