A React Native library for monitoring network connectivity status and connection type using Nitro Modules for high performance
npm install react-native-nitro-network-infoA React Native library for monitoring network connectivity status and connection type using Nitro Modules for high performance.
- 🚀 High performance with Nitro Modules
- 📡 Real-time network status monitoring
- 🔄 Connection type detection (WiFi, Cellular, etc.)
- 📱 Cross-platform support (iOS & Android)
- 🎣 React hooks friendly with listeners
- React Native v0.76.0 or higher
- Node 18.0.0 or higher
``bash`
bun add react-native-nitro-network-info react-native-nitro-modules@0.31.2
`typescript
import React, { useEffect, useState } from 'react';
import { View, Text } from 'react-native';
import NitroNetworkInfo, {
type NitroNetworkStatusInfo
} from 'react-native-nitro-network-info';
function App() {
const [networkInfo, setNetworkInfo] = useState
useEffect(() => {
// Add listener for network changes
const unsubscribe = NitroNetworkInfo.addListener((networkInfo) => {
setNetworkInfo(networkInfo);
});
// Cleanup listener on unmount
return () => {
unsubscribe();
};
}, []);
return (
Status: {NitroNetworkInfo.isConnected ? 'Connected' : 'Disconnected'}
Type: {NitroNetworkInfo.connectionType || 'Unknown'}
Network Info: {JSON.stringify(networkInfo, null, 2)}
);
}
`
#### isConnected: boolean
A readonly property that indicates whether the device is currently connected to a network.
`typescript`
const isConnected = NitroNetworkInfo.isConnected
#### connectionType: ConnectionType
A readonly property that returns the current connection type (e.g., 'wifi', 'cellular', 'ethernet', etc.).
`typescript`
const connectionType = NitroNetworkInfo.connectionType
#### addListener(listener: NetworkInfoListener): () => void
Adds a listener for network status changes. Returns a function to unsubscribe the listener.
Parameters:
- listener: NetworkInfoListener - Callback function that receives network status updates
Returns:
- () => void - Unsubscribe function
`typescript
const unsubscribe = NitroNetworkInfo.addListener((networkInfo) => {
console.log('Network status changed:', networkInfo)
})
// Later, when you want to stop listening
unsubscribe()
`
| Type | Description |
| ------------------------ | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| NetworkInfoListener | Callback function type that receives network status updates. Signature: (networkInfo: NitroNetworkStatusInfo) => void |ConnectionType
| | Enum or string type representing the available connection types (e.g., 'wifi', 'cellular', 'ethernet'). Exact values depend on platform implementation. |NitroNetworkStatusInfo` | Object containing detailed network status information passed to listeners. Structure depends on platform implementation and includes connection state and type details. |
|
Bootstrapped with create-nitro-module.
Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.