Ecosyste.ms: Awesome

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

Awesome Lists | Featured Topics | Projects

https://github.com/filipchalupa/use-connection-state

React hook to detect online and offline network state.
https://github.com/filipchalupa/use-connection-state

Last synced: about 15 hours ago
JSON representation

React hook to detect online and offline network state.

Awesome Lists containing this project

README

        

# Connection state hook [![npm](https://img.shields.io/npm/v/use-connection-state.svg)](https://www.npmjs.com/package/use-connection-state) ![npm type definitions](https://img.shields.io/npm/types/use-connection-state.svg)

React hook to detect online and offline network state.

You can play with it yourself here [filipchalupa.cz/use-connection-state](https://filipchalupa.cz/use-connection-state/).

## Installation

```bash
npm install use-connection-state
```

## Usage

```jsx
import { useConnectionState } from 'use-connection-state'

const App = () => {
const connection = useConnectionState()

return (

Connection state


Online: {connection === 'online' ? 'yes' : 'no'}



)
}
```

### Endpoints

```jsx
import { createUseConnectionStateHook } from 'use-connection-state'

// Add endpoints to detect connection state. The library will try to fetch these endpoints.
const useConnectionStateWithEndpoints = createUseConnectionStateHook({
endpoints: [location.href, 'https://www.google.com'],
})

const App = () => {
const connection = useConnectionStateWithEndpoints()

return (

Connection state


Online: {connection === 'online' ? 'yes' : 'no'}



)
}
```

## Development

- Install dependencies: `npm ci`
- Run: `npm run dev`