Created
January 23, 2025 13:58
-
-
Save sanusart/ef28642999d62639d8248ce8c8286772 to your computer and use it in GitHub Desktop.
is online hook #react #hooks
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
import { useEffect, useState } from 'react'; | |
export const useIsOnline = () => { | |
const [online, setOnline] = useState<boolean>(true); | |
const checkOnlineStatus = () => setOnline(navigator.onLine); | |
useEffect(() => { | |
window.addEventListener('online', checkOnlineStatus); | |
window.addEventListener('offline', checkOnlineStatus); | |
return () => { | |
window.removeEventListener('online', checkOnlineStatus); | |
window.removeEventListener('offline', checkOnlineStatus); | |
}; | |
}, []); | |
return online; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment