What is @react-native-community/netinfo?
@react-native-community/netinfo is a React Native library that provides information about the device's network connection state. It allows developers to detect the type of network connection (WiFi, cellular, etc.), whether the device is connected to the internet, and other related details.
What are @react-native-community/netinfo's main functionalities?
Check current network state
This feature allows you to fetch the current network state, including the connection type (e.g., WiFi, cellular) and whether the device is connected to the internet.
import NetInfo from '@react-native-community/netinfo';
NetInfo.fetch().then(state => {
console.log('Connection type', state.type);
console.log('Is connected?', state.isConnected);
});
Subscribe to network state changes
This feature allows you to subscribe to network state changes, so you can react to changes in the network connection in real-time.
import NetInfo from '@react-native-community/netinfo';
const unsubscribe = NetInfo.addEventListener(state => {
console.log('Connection type', state.type);
console.log('Is connected?', state.isConnected);
});
// Later, you can unsubscribe to stop listening for updates
unsubscribe();
Check if the device is connected to the internet
This feature allows you to check if the device is currently connected to the internet.
import NetInfo from '@react-native-community/netinfo';
NetInfo.fetch().then(state => {
console.log('Is connected?', state.isConnected);
});
Get detailed network information
This feature provides detailed information about the network connection, including whether the internet is reachable and additional details specific to the connection type.
import NetInfo from '@react-native-community/netinfo';
NetInfo.fetch().then(state => {
console.log('Connection type', state.type);
console.log('Is connected?', state.isConnected);
console.log('Is internet reachable?', state.isInternetReachable);
console.log('Details:', state.details);
});
Other packages similar to @react-native-community/netinfo
react-native-network-info
react-native-network-info is another React Native library that provides information about the device's network state. It offers functionalities such as getting the SSID of the connected WiFi network and the IP address of the device. However, it does not provide as comprehensive a set of features for monitoring network state changes as @react-native-community/netinfo.
react-native-connection-info
react-native-connection-info is a library that provides basic information about the network connection, such as the connection type and whether the device is connected to the internet. It is simpler and less feature-rich compared to @react-native-community/netinfo, making it suitable for applications that need only basic network information.
React Native Network Info API for iOS & Android
Getting started
yarn add @react-native-community/netinfo
or
npm install @react-native-community/netinfo --save
Mostly automatic installation
react-native link @react-native-community/netinfo
Manual installation
TODO
Usage
TODO