Skip to content

Instantly share code, notes, and snippets.

@benmotyka
Last active May 16, 2023 17:05
Show Gist options
  • Save benmotyka/5631f3e20f82e2c202cf067e92402bd7 to your computer and use it in GitHub Desktop.
Save benmotyka/5631f3e20f82e2c202cf067e92402bd7 to your computer and use it in GitHub Desktop.
Simple React hook that checks if user is online
useEffect(() => {
window.addEventListener('offline', () => {
setOnlineStatus(false);
});
window.addEventListener('online', () => {
setOnlineStatus(true);
});
return () => {
window.removeEventListener('offline', () => {
setOnlineStatus(false);
});
window.removeEventListener('online', () => {
setOnlineStatus(true);
});
}
}, [])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment