Skip to content

Instantly share code, notes, and snippets.

@brianbancroft
Created June 5, 2020 01:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save brianbancroft/d8ab79ce879bbb6d1e8e8ede29227c77 to your computer and use it in GitHub Desktop.
Save brianbancroft/d8ab79ce879bbb6d1e8e8ede29227c77 to your computer and use it in GitHub Desktop.
useOffline
const [online, setOnline] = useState(true)
useEffect(() => {
let offlineToast
const goOffline = () => {
setOnline(false)
}
const goOnline = () => {
setOnline(true)
}
window.addEventListener('offline', goOffline)
window.addEventListener('online', goOnline)
return () => {
window.removeEventListener('offline', goOffline)
window.removeEventListener('online', goOnline)
}
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment