# react-detect-offline

> Offline and Online components for React

Latest version **2.4.5** (published 2021-08-08) · MIT license · 0 weekly downloads

## Install

```sh
npm install react-detect-offline
pnpm add react-detect-offline
yarn add react-detect-offline
bun add react-detect-offline
```

## Health

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

Positive: has types package; no vulnerabilities; high quality score.

Warnings: low downloads; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 2.4.5 |
| Published | 2021-08-08 |
| First published | 2017-07-30 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | separate (@types/react-detect-offline) |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 15.1 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 1278 |
| Author | Chris Bolin |
| Maintainers | chrisbolin, cwise1989 |
| Keywords | react, offline, online, detect, disconnect |

## Links

- npm: https://www.npmjs.com/package/react-detect-offline
- Repository: https://github.com/cwise89/react-detect-offline
- Homepage: https://github.com/cwise89/react-detect-offline#readme
- Issues: https://github.com/cwise89/react-detect-offline/issues
- npm.io page: https://npm.io/package/react-detect-offline

## 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

- 2.4.5 (latest) — 2021-08-08
- 2.4.4 — 2021-05-13
- 2.4.3 — 2021-03-02
- 2.4.2 — 2021-02-20
- 2.4.1 — 2021-01-12
- 2.4.0 — 2019-05-11
- 2.3.0 — 2019-03-28
- 2.1.2 — 2018-07-18
- 2.1.1 — 2018-05-21
- 2.1.0 — 2018-03-30
- 2.0.1 — 2018-03-14
- 2.0.0 — 2018-03-02
- 1.0.6 — 2017-12-01
- 1.0.5 — 2017-09-27
- 1.0.4 — 2017-08-26
- … 4 more at https://npm.io/package/react-detect-offline/versions

## README

[![npm](https://img.shields.io/npm/v/react-detect-offline.svg)](https://www.npmjs.com/package/react-detect-offline)
[![npm downloads](https://img.shields.io/npm/dm/react-detect-offline.svg)](https://www.npmjs.com/package/react-detect-offline)
![gzip size](http://img.badgesize.io/https://unpkg.com/react-detect-offline?compression=gzip&label=gzip%20size)
![badges](https://img.shields.io/badge/license-MIT-lightgrey.svg)

### Offline and Online components for React

Components that track [offline and online state](https://developer.mozilla.org/en-US/docs/Online_and_offline_events). Render certain content only when online (or only when offline).

```jsx
import { Offline, Online } from "react-detect-offline";

const App = () => (
  <div>
    <Online>Only shown when you're online</Online>
    <Offline>Only shown offline (surprise!)</Offline>
  </div>
);
```

### Demo

Check out [chris.bolin.co/offline](https://chris.bolin.co/offline) for a simple example ([source code](https://github.com/chrisbolin/offline/blob/master/src/App.js)).

### Components

#### Simple

`<Online/>` and `<Offline/>` - Components that render their children only when the browser is online/offline.

```jsx
<Offline>You're offline right now. Check your connection.</Offline>
```

#### Advanced

`<Detector render={({ online }) => ...}/>` - Component that calls its `render` prop every time the connection state changes. The `render` prop is supplied with an object with an `online` boolean value. _Recommended for more complex cases, e.g. when styles need to be changed with connection status._

```jsx
<Detector
  render={({ online }) => (
    <div className={online ? "normal" : "warning"}>
      You are currently {online ? "online" : "offline"}
    </div>
  )}
/>
```

### Props

`<Online/>`, `<Offline/>`, and `<Detector/>` accept the following props:

| Prop               | Type        | Description                       | Default                        |
| ------------------ | ----------- | --------------------------------- | ------------------------------ |
| `polling`          | Obj or Bool | Config for polling fallback [1]   | [see below]                    |
| `polling.enabled`  | Boolean     | Force polling on or off           | Depends on the browser [1]     |
| `polling.url`      | String      | URL to pool for connection status | `"https://ipv4.icanhazip.com"` |
| `polling.interval` | Number      | How often (in ms) to poll         | `5000`                         |
| `polling.timeout`  | Number      | How long (in ms) before timeout   | `5000`                         |
| `onChange`         | Function    | Called when connection changes    | none                           |
| `children` [2]     | Element(s)  | Children **not Detector**         | none                           |
| `render` [3]       | Func        | Render function **Detector only** | none                           |

[1] Polling is only used as a fallback for browsers that don't support the `"online"` event. Currently these are Chrome on Windows, Firefox on Windows, and Chrome on Linux.

[2] `<Online/>` and `<Offline/>` only. `<Detector/>` will not render `children`.

[3] `<Detector/>` only

### Browser Support

The [web spec](https://developer.mozilla.org/en-US/docs/Online_and_offline_events) we rely on is supported by IE 9+, Chrome 14+, Firefox 41+, and Safari 5+ - that's [94% of worldwide (98% of US)](http://caniuse.com/#feat=online-status) browser traffic. A polling fallback is used for browsers that don't implement the spec in a useful way (see note [1] in the above Props section).

### Contributing

This package is maintained by [cwise89](https://github.com/cwise89) (and was initially created by [chrisbolin](https://github.com/chrisbolin)).

PRs are welcome!

* Test: `yarn test`
* Build: `yarn build`. Make sure you commit the build file (`dist/index.js`)

---
_Source: https://npm.io/package/react-detect-offline · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
