Created
October 4, 2021 12:10
-
-
Save ademilter/6355bba318e0005c719323e77c480dca to your computer and use it in GitHub Desktop.
react detecting focus of a browser window get data
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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