react-native-useonline
v1.0.2
Published
React Native online status hook
Downloads
9
Maintainers
Readme
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 }
Returns
The hook returns a boolean or null.
[true | false | null]