-
-
Save jgcmarins/3d4556c1a4b82226b9acf3565dcbf127 to your computer and use it in GitHub Desktop.
Alternative hook to check if the internet is reachable with @react-native-community/netinfo that does not return false on the initial results
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 { useState, useEffect } from 'react'; | |
import NetInfo from '@react-native-community/netinfo'; | |
const useInternetReachable = () => { | |
const [isReachable, setIsReachable] = useState(false); | |
const [isLoading, setIsLoading] = useState(true); | |
useEffect(() => { | |
const unsubscribe = NetInfo.addEventListener(({ isInternetReachable }) => { | |
if (typeof isInternetReachable !== 'boolean') return; | |
setIsLoading(false); | |
setIsReachable(isInternetReachable); | |
}); | |
return () => { | |
unsubscribe(); | |
}; | |
}, []); | |
return { isInternetReachable: isReachable, isLoading }; | |
}; | |
export default useInternetReachable; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment