Coder Social home page Coder Social logo

baptistelambert / react-native-netinfo Goto Github PK

View Code? Open in Web Editor NEW
11.0 3.0 0.0 42 KB

๐Ÿ“ฒ Notifies your app when the network connection goes online or offline.

Home Page: https://www.npmjs.com/package/react-native-netinfo

License: MIT License

JavaScript 100.00%
react react-native connectivity network render-prop

react-native-netinfo's Introduction

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.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.