how to check react-native network lost connection? I tried the below code and its throwing an error. I want to display and alert when app lost it's network connection?
CheckConnectivity = () => {
// For Android devices
if (Platform.OS === "android") {
NetInfo.isConnected.fetch().then(isConnected => {
console.log(isConnected,">>>")
if (isConnected) {
Alert.alert("You are online!");
} else {
Alert.alert("You are offline!");
}
});
} else {
// For iOS devices
NetInfo.isConnected.addEventListener(
"connectionChange",
this.handleFirstConnectivityChange
);
}
};
handleFirstConnectivityChange = isConnected => {
NetInfo.isConnected.removeEventListener(
"connectionChange",
this.handleFirstConnectivityChange
);
if (isConnected === false) {
Alert.alert("You are offline!");
} else {
Alert.alert("You are online!");
}
};
It looks like you are using the wrong API. For example, this is for WEB
If you check it here: https://github.com/react-native-netinfo/react-native-netinfo/blob/ba5c22cf2045b690243984a548ee25f4f6371e4a/src/internal/nativeModule.web.ts#L59
You need to follow this examples:
https://github.com/react-native-netinfo/react-native-netinfo/blob/master/example/IsConnected.tsx
https://github.com/react-native-netinfo/react-native-netinfo/blob/master/example/ConnectionInfoSubscription.tsx