I have an Expo app using the Managed workflow. The app needs to check whether an internet connection is available.
import { NetInfo } from 'react-native'
because that's deprecated.navigator.onLine
because that global variable doesn't seem to be available.What should I do?
@react-native-community/netinfo allows you to get information about connection type and connection quality.
Allow standalone apps to load and function with no internet connection | Voters | Expo. Standalone apps should work without an internet connection, or if Expo's services are unavailable. Android standalone apps already bundle a project's JavaScript and manifest, but static (image, font, etc.) assets are not bundled yet ...
Enable the wireless connection on your device and select "EXPO", internet access is enabled. The complimentary Wi-Fi service ("Service") is provided free of charge by Shenyang New World EXPO ("EXPO") to the customers of EXPO ("Subscriber").
Expo SDK 34 has already included NetInfo API
.
You can check their documentation for SDK 34 here https://docs.expo.io/versions/v34.0.0/sdk/netinfo
Here is the link for documentation for latest version
It's really hard to define if a device has internet or not stackoverflow.com/a/189443/7602110, just by having failed XHR requests you can say that you have internet, but isn't that reliable. You would like to check with some reliables websites like google.com, I have come with a work-around but I don't actually recommend it, is up to you.
You can use the Linking.canOpenUrl()
method from React Native itself, which will return a Promise object. When it is determined whether or not the given URL can be handled, the promise is resolved and the first parameter is whether or not it can be opened.
Then add a request and if the response status it's 200
you should have internet.
import React, { Component } from 'react';
import { Button, Text, View, StyleSheet, Linking } from 'react-native';
export default class App extends Component {
state = {
connection: false,
url: 'https://google.com',
};
checkInternt = () => {
Linking.canOpenURL(this.state.url).then(connection => {
if (!connection) {
this.setState({ connection: false });
} else {
fetch(this.state.url).then(res =>
this.setState({ connection: res.status !== 200 ? false : true })
);
}
});
};
componentDidMount() {
this.checkInternt();
}
handlePress = () => {
this.setState({
url:
this.state.url === 'https://google.com'
? 'http://someweirdurlthatdoesntwork.com'
: 'https://google.com',
});
this.checkInternt();
};
render() {
return (
<View style={styles.container}>
<Text>
Connection:
<Text style={{ color: this.state.connection ? 'green' : 'red' }}>
{` ${this.state.connection}`}
</Text>
</Text>
<Text>{this.state.url.replace(/\https?:\/\//g, '')}</Text>
<Button onPress={this.handlePress} title="Change server url" />
</View>
);
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'space-around',
alignItems: 'center',
},
});
Check the snack: snack.expo.io/@abranhe/check-internet
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With