Last active
March 10, 2019 22:23
-
-
Save joefazz/7040ad1d7af8a37cabc67abf9f85fd39 to your computer and use it in GitHub Desktop.
PageVisibilityHook
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
import React, { useEffect } from 'react'; | |
function usePageVisibility(onVisible, onHidden) { | |
const visibilityChange = 'visibilitychange'; | |
function onVisibilityChange() { | |
if (document.hidden) { | |
onHidden(); | |
} else { | |
onVisible(); | |
} | |
} | |
useEffect(() => { | |
document.addEventListener(visibilityChange, () => onVisibilityChange()); | |
return function clean() { | |
document.removeEventListener(visibilityChange, () => onVisibilityChange()); | |
}; | |
}, []); | |
} | |
export default usePageVisibility; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment