Skip to content

Instantly share code, notes, and snippets.

@MKS-01
Last active September 7, 2020 18:49
Show Gist options
  • Save MKS-01/f65e1ca92be837cde7f26ec4af2a9916 to your computer and use it in GitHub Desktop.
Save MKS-01/f65e1ca92be837cde7f26ec4af2a9916 to your computer and use it in GitHub Desktop.
Network Error Connection Check hook
import {useState, useEffect} from 'react';
import NetInfo from '@react-native-community/netinfo';
let currentNetwork;
NetInfo.fetch().then((state) => {
currentNetwork = state.isConnected;
});
const CheckConnection = () => {
const [netInfo, setNetInfo] = useState(currentNetwork);
useEffect(() => {
const unsubscribe = NetInfo.addEventListener((state) => {
// console.log("Connection type", state.type);
// console.log("Is connected?", state.isConnected);
setNetInfo(state.isConnected);
});
return () => unsubscribe();
}, []);
return netInfo;
};
export default CheckConnection;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment