react-native-netinfo
Notifies your app when the network connection goes online or offline.
Inspired by react-network and react-native-offline, designed with a similar API to the former for when you need a simpler and lighter package than the latter.
Installation
npm install react-native-netinfo
# or with yarn
yarn add react-native-netinfo
Usage
With a render prop
; const App = <View> <NetInfoProvider onChange= { console; console; } render= isConnected ? <ReactFragment> <Text>Wonderful you are connected!</Text> <Text>Connection type: connectionInfotype</Text> <Text> Effective connection type:connectionInfoeffectiveType </Text> </ReactFragment> : <Text>It looks like you encounter connectivity problems</Text> /> </View>;
With children as a function
; const App = <View> <NetInfoProvider onChange= { console; console; } > isConnected ? <ReactFragment> <Text>Wonderful you are connected!</Text> <Text>Connection type: connectionInfotype</Text> <Text> Effective connection type:connectionInfoeffectiveType </Text> </ReactFragment> : <Text>It looks like you encounter connectivity problems</Text> </NetInfoProvider> </View>;
With component injection
; const ConnectedComponent = isConnected ? <ReactFragment> <Text>Wonderful you are connected!</Text> <Text>Connection type: connectionInfotype</Text> <Text>Effective connection type:connectionInfoeffectiveType</Text> </ReactFragment> : <Text>It looks like you encounter connectivity problems</Text> ; const App = <View> <NetInfoProvider onChange= { console; console; } component=ConnectedComponent /> </View>;
NB: you should not set both component and render props. If you were to do this, the render prop would be ignored.
Constants
This package also exposes constants for connection info's types and effective types.
You can use them like so:
; const App = <View> <Text>CONSTANTSCONNECTION_INFOTYPESWIFI</Text> <Text>CONSTANTSCONNECTION_INFOEFFECTIVE_TYPES'4G'</Text> </View>;
You can find the full list of types and effective types in the official React Native documentation about NetInfo.