# @sooni-hooks/use-network

> React Hook to detect user's network state

Latest version **1.0.1** (published 2021-04-05) · MIT license · 0 weekly downloads

## Install

```sh
npm install @sooni-hooks/use-network
pnpm add @sooni-hooks/use-network
yarn add @sooni-hooks/use-network
bun add @sooni-hooks/use-network
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.1 |
| Published | 2021-04-05 |
| First published | 2021-03-22 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 3.2 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 1 |
| Author | soonitoon |
| Maintainers | soonitoon |
| Keywords | React, Hook, network, useNetwork |

## Links

- npm: https://www.npmjs.com/package/@sooni-hooks/use-network
- Repository: https://github.com/soonitoon/React_Hooks
- Homepage: https://github.com/soonitoon/React_Hooks#readme
- Issues: https://github.com/soonitoon/React_Hooks/issues
- npm.io page: https://npm.io/package/@sooni-hooks/use-network

## Alternatives

- [mobx-react](https://npm.io/package/mobx-react.md) — 2.8M weekly downloads
- [rc-tree](https://npm.io/package/rc-tree.md) — 2.6M weekly downloads
- [@react-oauth/google](https://npm.io/package/@react-oauth/google.md) — 1.3M weekly downloads
- [@wagmi/connectors](https://npm.io/package/@wagmi/connectors.md) — 877.0K weekly downloads
- [vee-validate](https://npm.io/package/vee-validate.md) — 836.4K weekly downloads

## Recent versions

- 1.0.1 (latest) — 2021-04-05
- 1.0.0 — 2021-03-22

## README

# useNetwork
React-Hook that can catch user's network status.

## Notice
**useNetwork** is custom React-Hook; So it works on only **React environment**.

## Installation
1. `$ npm install @sooni-hooks/use-network`
2. Add `import useNetwork from "@sooni-hooks/use-network"` in your script.
3. Done!

## How to use
**useNetwork** takes `onChange function` as argument. This function takes the Boolean value. And it works according to the Boolean value. Example is:
```js
const onChange = (isOnline) => {
  if (isOnline){
    console.log("Online");
  } else {
    console.log("Offline");
  }
}
```
Also useNetwork returns `isOnline`. This is Boolean value. If user's network is connected, `isOnline`'s value is `true`. If user's network is unconnected, `isOnline`'s value is `false`.

## Example
```js
function App() {
  const onChange = (isOnline) => {
    console.log(isOnline ? "online" : "offline");
  };
  const isOnline = useNetwork(onChange);
  return (
    <div className="App">
      <h1>{JSON.stringify(isOnline)}</h1>
    </div>
  );
}
```

## Development environment setting

1. First, you need to install **NPM**
   - Linux : `$ sudo apt install npm`
   - Windows : Go to download link https://nodejs.org/en/download/

2. Install **react** and **react-dom**
   `$ npm i react react-dom`

## Full code
```js
import { useEffect, useState } from "react";

const useNetwork = (onChange) => {
  const [isOnline, setIsOnline] = useState(navigator.onLine);
  const handleChange = () => {
    if (typeof onChange === "function") {
      onChange(navigator.onLine);
    }
    setIsOnline(navigator.onLine);
  };
  useEffect(() => {
    window.addEventListener("online", handleChange);
    window.addEventListener("offline", handleChange);
    return () => {
      window.removeEventListener("online", handleChange);
      window.removeEventListener("offline", handleChange);
    };
  }, []);
  return isOnline;
};

export default useNetwork;
```

---
_Source: https://npm.io/package/@sooni-hooks/use-network · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
