1.0.2 • Published 2 years ago

react-native-useonline v1.0.2

Weekly downloads
-
License
MIT
Repository
github
Last release
2 years ago

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]