Last active
September 7, 2020 18:49
-
-
Save MKS-01/f65e1ca92be837cde7f26ec4af2a9916 to your computer and use it in GitHub Desktop.
Network Error Connection Check hook
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'; | |
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