Skip to content

Instantly share code, notes, and snippets.

@julioxavierr
Last active November 23, 2020 18:53
Show Gist options
  • Save julioxavierr/12ef1f153d6acc01e79d8cb8a55c0efd to your computer and use it in GitHub Desktop.
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
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