usePageVisibility tracks the page visibility state (visible/hidden) via the visibilitychange event. It returns a hybrid structure with the current isVisible flag and (optionally) invokes a callback on every visibility change.
- Tuple:
[isVisible] - Object:
{ isVisible }
function usePageVisibility(
callback?: UsePageVisibilityCallback,
): UsePageVisibilityReturn;-
Parameters
callback?— a function called on each visibility change:(isVisible: boolean) => void.
-
Returns:
UsePageVisibilityReturn— a hybrid structure withisVisible: booleanas property/tuple element.
import { usePageVisibility } from '@webeach/react-hooks/usePageVisibility';
export function PageActivityBadge() {
const { isVisible } = usePageVisibility();
return <div>{isVisible ? '🟢 Tab is active' : '⚪️ Tab is inactive'}</div>;
}import { usePageVisibility } from '@webeach/react-hooks/usePageVisibility';
export function Spinner() {
const { isVisible } = usePageVisibility();
return <div className={isVisible ? 'spinner' : 'spinner spinner--paused'} />;
}-
isVisiblesemantics- Reflects
document.visibilityState === 'visible'and updates on thevisibilitychangeevent.
- Reflects
-
Optional callback
- On every change,
callback?.(isVisible)is called. The callback always fires even if the component doesn’t readisVisiblein the UI.
- On every change,
-
Updates only when used
- If
isVisibleisn’t read (e.g., you only rely on the callback), an extra re-render is not triggered.
- If
-
SSR/ISR safety
- On the server,
isVisibleis consideredtrue; the event subscription is attached only after mount. Right after hydration, the real value may arrive and cause an update.
- On the server,
-
Stable interface
- The returned structure consistently exposes only
isVisible; convenient for both tuple and object destructuring.
- The returned structure consistently exposes only
- Pause polling/timers/animations on a hidden tab.
- Disable heavy computations and network requests while the user is away.
- Show "online/inactive" indicators or badges.
- If your logic doesn’t depend on tab visibility.
- If you need more detailed activity telemetry (window focus,
pointermove, other events) — combine with dedicated hooks/events.
-
Expecting a re-render without reading
isVisible- If you don’t use
isVisiblein markup/logic, the component won’t re-render — use the callback for side effects.
- If you don’t use
-
Heavy work inside the callback
- Avoid long-running work in the visibility handler; offload to deferred tasks/workers.
-
Relying on the SSR initial value
- Remember that on the server
isVisible = true; immediately after mount the value can change.
- Remember that on the server
Exported types
-
UsePageVisibilityCallback- Callback invoked on document visibility changes:
(isVisible: boolean) => void.
- Callback invoked on document visibility changes:
-
UsePageVisibilityReturn- Hybrid type: tuple
[isVisible]and object{ isVisible }.
- Hybrid type: tuple
-
UsePageVisibilityReturnObject- Object form:
{ isVisible: boolean }.
- Object form:
-
UsePageVisibilityReturnTuple- Tuple form:
[isVisible: boolean].
- Tuple form: