Skip to main content
100%

Hook that emits document visibility.

✓ Published0🌍 Public
Aabonello
Last edited Jun 25, 2021
Created on Dec 30, 2021

This example demonstrates a React hook that tracks whether the browser tab is currently visible to the user. It shows the document’s `visibilityState` and returns a boolean indicating if the page is in the foreground. The hook uses React’s `useState` and `useEffect` to initialize the state and subscribe to the `visibilitychange` event on the document. When the tab’s visibility changes, the event listener updates the state accordingly, and the effect cleans up the listener on unmount.

AI-generated description

Similar vizzes