is-internet-or-lost
Version:
A simple utility to check network status (online/offline) and listen for changes in the connection.
57 lines (40 loc) • 1.57 kB
Markdown
`is-internet-or-lost` is a simple utility to check your network status (online/offline) and listen for any changes in your internet connection. It helps in determining whether the device is connected to the internet or not in real-time.
## Features
- Check if the device is connected to the internet.
- Detect online/offline status changes.
- Easy to integrate and use.
## Installation
To install `is-internet-or-lost`, run the following command:
```bash
npm install is-internet-or-lost
```
## Usage
### useNetworkStatus Hook
You can use the `useNetworkStatus` function to track the network status (online/offline) and listen for any changes. This function is designed to be used with event listeners for `online` and `offline` events.
#### Example
```javascript
// index.js
import { useNetworkStatus } from 'is-internet-or-lost';
// Create a component or function to handle the network status
function App() {
const onNetworkChange = (status) => {
if (status === 'online') {
console.log('You are online!');
} else {
console.log('You are offline!');
}
};
// Initialize the network status listener
const { mount, unmount } = useNetworkStatus(onNetworkChange);
// Mount the listeners when the component is loaded
useEffect(() => {
mount();
// Cleanup the listeners when the component is unmounted
return () => {
unmount();
};
}, []); // Empty array ensures it only runs once when component mounts
return <div>Check your network status in the console.</div>;
}