译零 TransZero · TanStack Query v3 中文文档
主站

FocusManager

The FocusManager manages the focus state within React Query.

It can be used to change the default event listeners or to manually change the focus state.

Its available methods are:

focusManager.setEventListener

setEventListener can be used to set a custom event listener:

JavaScript
1import { focusManager } from "react-query";2 3focusManager.setEventListener((handleFocus) => {4  // Listen to visibilitychange and focus5  if (typeof window !== "undefined" && window.addEventListener) {6    window.addEventListener("visibilitychange", handleFocus, false);7    window.addEventListener("focus", handleFocus, false);8  }9 10  return () => {11    // Be sure to unsubscribe if a new handler is set12    window.removeEventListener("visibilitychange", handleFocus);13    window.removeEventListener("focus", handleFocus);14  };15});

focusManager.setFocused

setFocused can be used to manually set the focus state. Set undefined to fallback to the default focus check.

JavaScript
1import { focusManager } from "react-query";2 3// Set focused4focusManager.setFocused(true);5 6// Set unfocused7focusManager.setFocused(false);8 9// Fallback to the default focus check10focusManager.setFocused(undefined);

Options

  • focused: boolean | undefined

focusManager.isFocused

isFocused can be used to get the current focus state.

JavaScript
1const isFocused = focusManager.isFocused();

本页为 TanStack Query 文档(React 版)的非官方中文翻译,原文由上游团队维护,遵循 MIT 许可。 译文如有偏差,以英文原文为准。

本页译文取自社区中文版 react-query-web-i18n(MIT),非本站翻译,已停止更新,内容可能与当前 API 有出入。

译自上游 (未记录版本)