Skip to content

Commit

Permalink
Added 📴 useOfflineStatus (#41)
Browse files Browse the repository at this point in the history
  • Loading branch information
aromalanil authored May 12, 2021
1 parent d039fe4 commit 8e30bc2
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 2 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ yarn add use-custom-hooks
- [useGeoLocation](https://github.com/aromalanil/useCustomHooks/tree/master/docs#-usegeolocation)
- [useLocalStorage](https://github.com/aromalanil/useCustomHooks/tree/master/docs#-uselocalstorage)
- [useMousePosition](https://github.com/aromalanil/useCustomHooks/tree/master/docs#-usemouseposition)
- [useOfflineStatus](https://github.com/aromalanil/useCustomHooks/tree/master/docs#-useofflinestatus)

## 📄 Documentation
For documentation, examples and other details refer [Documentation](https://github.com/aromalanil/useCustomHooks/tree/master/docs)
Expand Down
28 changes: 28 additions & 0 deletions docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
- [useMousePosition](#-usemouseposition)
- [useGeoLocation](#-usegeolocation)
- [usePrevious](#-useprevious)
- [useOfflineStatus](#-useofflinestatus)

</br>

Expand Down Expand Up @@ -409,6 +410,33 @@ function App() {

</br>

## 📴 useOfflineStatus

Returns a boolean state which represents if the device is offline

### Usage

```jsx
import React from "react";
import { useOfflineStatus } from "use-custom-hooks";

const LocalValue = () => {
const isDeviceOffline = useOfflineStatus();

return <p>Device is currently {isDeviceOffline ? "Offline" : "Online"}</p>;
};
```

### Parameters

None : This hooks takes no parameters.

### Return value

`isDeviceOffline` (_boolean_) : A state representing if device is offline.

</br>


# Contribution Guidelines

Expand Down
29 changes: 29 additions & 0 deletions hooks/useOfflineStatus.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { useEffect, useState } from 'react';

/**
*
* Custom hook to know the current status of the device (online/offline).
*
* @return {Boolean} If the device is offline or not
*/
const useOfflineStatus = () => {
const [isDeviceOffline, setDeviceOffline] = useState(false);

useEffect(() => {
const networkStatusListener = () => {
setDeviceOffline(!navigator.onLine);
};

window.addEventListener('online', networkStatusListener);
window.addEventListener('offline', networkStatusListener);

return () => {
window.removeEventListener('online', networkStatusListener);
window.removeEventListener('offline', networkStatusListener);
};
}, []);

return isDeviceOffline;
};

export default useOfflineStatus;
1 change: 1 addition & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ export { default as useDebounce } from './hooks/useDebounce';
export { default as useGeoLocation } from './hooks/useGeoLocation';
export { default as useLocalStorage } from './hooks/useLocalStorage';
export { default as useMousePosition } from './hooks/useMousePosition';
export { default as useOfflineStatus } from './hooks/useOfflineStatus';
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "use-custom-hooks",
"version": "1.8.1",
"version": "1.9.0",
"description": "A collection of Custom Hooks for React.",
"main": "index.js",
"scripts": {
Expand Down

0 comments on commit 8e30bc2

Please sign in to comment.