Socket
Book a DemoInstallSign in
Socket

react-native-netinfo

Package Overview
Dependencies
Maintainers
1
Versions
2
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

react-native-netinfo

Notifies your app when the network goes offline and back online.

1.1.0
latest
Source
npmnpm
Version published
Weekly downloads
679
-41.01%
Maintainers
1
Weekly downloads
 
Created
Source

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

import { NetInfoProvider } from 'react-native-netinfo';

const App = () => (
  <View>
    <NetInfoProvider
      onChange={({ isConnected, connectionInfo }) => {
        console.log(isConnected);
        console.log(connectionInfo);
      }}
      render={({ isConnected, connectionInfo }) =>
        isConnected ? (
          <React.Fragment>
            <Text>Wonderful, you are connected!</Text>
            <Text>Connection type: {connectionInfo.type}</Text>
            <Text>
              Effective connection type:{connectionInfo.effectiveType}
            </Text>
          </React.Fragment>
        ) : (
          <Text>It looks like you encounter connectivity problems.</Text>
        )
      }
    />
  </View>
);

With children as a function

import { NetInfoProvider } from 'react-native-netinfo';

const App = () => (
  <View>
    <NetInfoProvider
      onChange={({ isConnected, connectionInfo }) => {
        console.log(isConnected);
        console.log(connectionInfo);
      }}
    >
      {({ isConnected, connectionInfo }) =>
        isConnected ? (
          <React.Fragment>
            <Text>Wonderful, you are connected!</Text>
            <Text>Connection type: {connectionInfo.type}</Text>
            <Text>
              Effective connection type:{connectionInfo.effectiveType}
            </Text>
          </React.Fragment>
        ) : (
          <Text>It looks like you encounter connectivity problems.</Text>
        )
      }
    </NetInfoProvider>
  </View>
);

With component injection

import { NetInfoProvider } from 'react-native-netinfo';

const ConnectedComponent = ({ isConnected, connectionInfo }) =>
  isConnected ? (
    <React.Fragment>
      <Text>Wonderful, you are connected!</Text>
      <Text>Connection type: {connectionInfo.type}</Text>
      <Text>Effective connection type:{connectionInfo.effectiveType}</Text>
    </React.Fragment>
  ) : (
    <Text>It looks like you encounter connectivity problems.</Text>
  );

const App = () => (
  <View>
    <NetInfoProvider
      onChange={({ isConnected, connectionInfo }) => {
        console.log(isConnected);
        console.log(connectionInfo);
      }}
      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:

import { CONSTANTS } from 'react-native-netinfo';

const App = () => (
  <View>
    <Text>{CONSTANTS.CONNECTION_INFO.TYPES.WIFI}</Text>
    <Text>{CONSTANTS.CONNECTION_INFO.EFFECTIVE_TYPES['4G']}</Text>
  </View>
);

You can find the full list of types and effective types in the official React Native documentation about NetInfo.

FAQs

Package last updated on 15 Feb 2018

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

About

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.

  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc

U.S. Patent No. 12,346,443 & 12,314,394. Other pending.