Licence
MIT
Version
0.1.0
Deps
0
Size
30 kB
Vulns
0
Weekly
0
react-native-network-state
A lightweight React Native library for monitoring network connectivity and internet reachability with a simple, developer-friendly API.
Features
- Get the current network state on demand
- Subscribe to connectivity changes in real time
- React hook for components
- Android and iOS support via Turbo Modules (New Architecture)
- Web fallback using
navigator.onLine
Installation
npm install react-native-network-state
iOS
cd ios && pod install
No extra permissions are required on iOS.
Android
The library automatically adds ACCESS_NETWORK_STATE to your app manifest.
Usage
Fetch current state
import { getNetworkState } from 'react-native-network-state';
const state = await getNetworkState();
console.log(state.isConnected); // true | false
console.log(state.isInternetReachable); // true | false | null
console.log(state.type); // 'wifi' | 'cellular' | 'ethernet' | 'none' | 'unknown'
Listen for changes
import { addNetworkStateListener } from 'react-native-network-state';
const unsubscribe = addNetworkStateListener((state) => {
console.log('Network changed:', state);
});
// Later
unsubscribe();
React hook
import { useNetworkState } from 'react-native-network-state';
function NetworkBanner() {
const { isConnected, isInternetReachable, type } = useNetworkState();
if (!isConnected) {
return <Text>You are offline</Text>;
}
if (isInternetReachable === false) {
return <Text>Connected, but internet is unreachable</Text>;
}
return <Text>Online via {type}</Text>;
}
API
getNetworkState(): Promise<NetworkState>
Returns the current network state.
addNetworkStateListener(handler): () => void
Registers a listener for network state changes. Returns an unsubscribe function.
useNetworkState(): NetworkState
React hook that returns the current state and updates automatically.
NetworkState
| Property | Type | Description |
|---|---|---|
isConnected |
boolean |
Whether a network interface is available |
isInternetReachable |
boolean | null |
Whether the internet is reachable (null while checking) |
type |
NetworkType |
Connection type: wifi, cellular, ethernet, none, or unknown |
Requirements
- React Native 0.76+
- New Architecture enabled (Turbo Modules)
Contributing
License
MIT