react-native-useonline
TypeScript icon, indicating that this package has built-in type declarations

1.0.2 • Public • Published

react-native-useonline

React Native online status hook

A hook that reflects whether the device is online (is connected to the internet).
Works with expo! ✌️

The useOnline hook updates on both event triggers and an interval.
The hook pings a website/endpoint and if the server responds with 200 the hook will return true.

Installation

npm install react-native-useonline

Usage

import React from "react";
import {
    View,
    Text
} from "react-native";
import useOnline from "react-native-useonline";

const App = () => {
    const online = useOnline();

    return (
        <View
            style={{
                flex: 1,
                justifyContent: "center",
                alignItems: "center"
            }}
        >
            <Text>
                {`Is online: ${online}`}
            </Text>
        </View>
    );
}

export default App;

Custom usage

import React from "react";
import {
    View,
    Text
} from "react-native";
import useOnline from "react-native-useonline";

const App = () => {
    const online = useOnline({ url: "https://duckduckgo.com", pingInterval: 600000 });

    return (
        <View
            style={{
                flex: 1,
                justifyContent: "center",
                alignItems: "center"
            }}
        >
            <Text>
                {`Is online: ${online}`}
            </Text>
        </View>
    );
}

export default App;

Hook

Arguments

  • options: IOptions { url: string, pingInterval: number }

``` url ``` defaults to https://google.com
``` pingInterval ``` defaults to 30000 milliseconds

Returns

The hook returns a boolean or null.
[true | false | null]

Package Sidebar

Install

npm i react-native-useonline

Weekly Downloads

2

Version

1.0.2

License

MIT

Unpacked Size

8.21 kB

Total Files

10

Last publish

Collaborators

  • lukaswass