I want to check the internet connectivity when starting the app, then ask the user to connect to the internet if he doesn't
Asked
Active
Viewed 6,318 times
3
-
1I believe this is a duplicate. Check this question: https://stackoverflow.com/questions/57296756/how-to-check-internet-connection-in-react-native-application-for-both-ios-and-an – EVeras Oct 10 '20 at 16:18
3 Answers
9
You should use the "@react-native-community/netinfo" library. NetInfo used to be part of the react-native, but then it got separated out of the core. If you want to observe network state changes just use the provided addEventListener method.
import NetInfo from "@react-native-community/netinfo";
NetInfo.fetch().then(state => {
console.log("Connection type", state.type);
console.log("Is connected?", state.isConnected);
});
const unsubscribe = NetInfo.addEventListener(state => {
console.log("Connection type", state.type);
console.log("Is connected?", state.isConnected);
});
// Unsubscribe
unsubscribe();

twboc
- 1,452
- 13
- 17
2
import NetInfo from '@react-native-community/netinfo';
NetInfo.fetch().then((state) => {
console.log('Connection type', state.type);
console.log('Is connected?', state.isConnected);
});
const unsubscribe = NetInfo.addEventListener((state) => {
console.log('Connection type', state.type);
console.log('Is connected?', state.isConnected);
});

John Michael Manlupig
- 139
- 1
- 8
0
For my project I've used NetInfo, according to state of connection I'm toggling No Internet screen
useEffect(() => {
NetInfo.fetch().then((state) => {
console.log('Is connected?', state.isConnected);
setShowNoInterNetPage(!state?.isConnected)
});
const unsubscribe = NetInfo.addEventListener((state) => {
console.log('Is connected?', state.isConnected);
});
return () => unsubscribe.remove();
}, [])

Atul Tiwaree
- 27
- 1
- 5
-
You can take a look here: https://stackoverflow.com/questions/77029163/react-native-check-if-there-is-internet-connection-via-listener – Paul Sep 02 '23 at 16:48