Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/dashed/react-offline

React component that notifies when browser is either offline or online.
https://github.com/dashed/react-offline

offline online react

Last synced: about 2 months ago
JSON representation

React component that notifies when browser is either offline or online.

Awesome Lists containing this project

README

        

# react-offline [![Build Status](https://travis-ci.org/dashed/react-offline.svg)](https://travis-ci.org/dashed/react-offline) [![npm version](https://img.shields.io/npm/v/react-offline.svg?style=flat)](https://www.npmjs.com/package/react-offline)

[![Greenkeeper badge](https://badges.greenkeeper.io/dashed/react-offline.svg)](https://greenkeeper.io/)

> React component that notifies when browser is either offline or online.

## Install

```sh
yarn add react-offline
# or
npm install react-offline
```

## Usage

```js
// 3rd-party imports

import ReactDOM from "react-dom";
import React, { Component } from "react";

import Offline from "react-offline";

// function as child component

ReactDOM.render(
console.log({ isOffline, isOnline })}
>
{({ isOffline, isOnline }) => {
return isOffline ? (

{"I am offline"}

) : (
{"I am online"}

);
}}
,
mountNode
);

// render prop

ReactDOM.render(
console.log({ isOffline, isOnline })}
render={({ isOffline, isOnline }) => {
return isOffline ?

I am offline
:
I am online
;
}}
/>,
mountNode
);
```

## Props

### `render` (optional)

An optional function that is called whenever the browser's network connectivity has changed (e.g. switching between online and offline).

The `render` function is invoked with an object argument: `({ isOffline, isOnline })`.

It's expected that `render` function returns a single React element.
This has same API semantics as [`React.Component.render()`](https://facebook.github.io/react/docs/react-component.html#render).

If `render` function is given, it has precedence over any given child component:

```js
{
return

"I take precedence over any function as child component."
;
}}
>
{() => {
return
"I will not render."
;
}}

```

### Function as child component (optional)

Same as `render` prop function (see above).

If `render` function is not given, then the child component will be invoked as a function.

The child component function is invoked with an object argument: `({ isOffline, isOnline })`.

```js

{({ isOffline, isOnline }) => {
return isOffline ?

{"I am offline"}
:
{"I am online"}
;
}}

```

### `onChange` (optional)

An optional function that is called whenever the browser's network connectivity has changed (e.g. switching between online and offline).

The `onChange` function is invoked with an object argument: `({ isOffline, isOnline })`.

# License

MIT.