Created
May 10, 2022 07:45
-
-
Save hmelenok/3c16c58cb3230838ff04a156d35cebb9 to your computer and use it in GitHub Desktop.
visibilitychange with react useSyncExternalStore
This file contains hidden or 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 function useVisibilityChange(serverFallback: boolean) { | |
const getServerSnapshot = () => serverFallback; | |
const [getSnapshot, subscribe] = useMemo(() => { | |
return [ | |
() => document.visibilityState === 'visible', | |
(notify: () => void) => { | |
window.addEventListener('visibilitychange', notify); | |
return () => { | |
window.removeEventListener('visibilitychange', notify); | |
}; | |
}, | |
]; | |
}, []); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment