JSPM

use-connection-state

0.2.0
  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 125
  • Score
    100M100P100Q80212F
  • License ISC

React hook to detect online and offline network state.

Package Exports

  • use-connection-state
  • use-connection-state/dist/index.es.js
  • use-connection-state/dist/index.js

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (use-connection-state) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

Connection state hook npm npm type definitions

React hook to detect online and offline network state.

You can play with it yourself here filipchalupa.cz/use-connection-state.

Installation

npm install use-connection-state

Usage

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

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

    return (
        <main>
            <h1>Connection state</h1>
            <p>Online: {connection === 'online' ? 'yes' : 'no'}</p>
        </main>
    )
}

Endpoints

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 (
        <main>
            <h1>Connection state</h1>
            <p>Online: {connection === 'online' ? 'yes' : 'no'}</p>
        </main>
    )
}

Development

  • Install dependencies: npm ci
  • Run: npm run dev