useDocumentVisibility

A Hook can tell if the page is visible.

The API is different from ahooks.

visibilityState API

Examples

Basic usage

API

const { isVisible, visibilityState } = useDocumentVisibility({
  onHidden,
  onVisible,
  onChange,
}: UseDocumentVisibilityOptions);
export interface UseDocumentVisibilityOptions {
  onHidden?: () => void;
  onVisible?: () => void;
  onChange?: (visibilityState: VisibilityState) => void;
}

Params

PropertyDescriptionTypeDefault
onHiddenDocument hidden callbackFunction() => void
onVisibleDocument visible callbackFunction() => void
onChangeDocument visibility change callbackFunction(visibilityState: VisibilityState) => void

Result

PropertyDescriptionTypeDefault
visibilityStateDocument's visible statestringhidden | visible
isVisibleDetermine if the document is visiblebooleandocument.visibilityState

最近更新: