Skip to main content
100%

Hook that emits when the window has focus.

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

This example demonstrates a React hook that tracks whether the browser window currently has focus, returning a boolean value that updates in real time. It uses the `useState` and `useEffect` hooks from React to manage state and lifecycle events. The hook listens for the native `focus` and `blur` events on the `window` object, updating the state accordingly and cleaning up event listeners on unmount. It reads the initial focus state via `document.hasFocus()` and returns the current focus status, enabling components to react to visibility changes.

AI-generated description

Similar vizzes