Last active
November 23, 2020 18:53
-
-
Save julioxavierr/12ef1f153d6acc01e79d8cb8a55c0efd 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