Skip to content

Instantly share code, notes, and snippets.

@ademilter
Created October 4, 2021 12:10
Show Gist options
  • Save ademilter/6355bba318e0005c719323e77c480dca to your computer and use it in GitHub Desktop.
Save ademilter/6355bba318e0005c719323e77c480dca to your computer and use it in GitHub Desktop.
react detecting focus of a browser window get data
export default function Component() {
const [isFocus, setIsFocus] = useState(true)
const { getData } = useContext(RedisDatabaseStoreContext)
useInterval(async () => {
if (!isFocus) return
await getData()
}, 5000)
useEffect(() => {
document.onfocus = () => setIsFocus(true)
document.onblur = () => setIsFocus(false)
}, [])
return null
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment