2.0.1 • Published 3 years ago

use-network-api v2.0.1

Weekly downloads
-
License
MIT
Repository
github
Last release
3 years ago

use-network-api

NPM version NPM Weekly Downloads License

A simple JavaScript utility to access the network api.

Install with npm, or Yarn:

# via npm
npm install use-network-api

# or Yarn (note that it will automatically save the package to your `dependencies` in `package.json`)
yarn add use-network-api

Usage with React.js

import { useNetworkApi } from "use-network-api";

function App() {
  const { getBrowser, connection, isOnline } = useNetworkApi();
  return (
    <div>
      <p>Browser: {getBrowser()}</p>
      <p>IsOnline: {isOnline() ? 'Yes' : 'No'}</p>
      <p>Connection Type: {connection?.effectiveType}</p>
    </div>
  );
}

export default App;

Call a function for when the browser does not support the feature

import { useNetworkApi } from "use-network-api";

const handleBrowserNotSupport = () => {
  alert('Not support :(')
}

function App() {  
  const { getBrowser, connection, isOnline } = useNetworkApi(handleBrowserNotSupport);
  
  return (
    <div>
      <p>Browser: {getBrowser()}</p>
      <p>IsOnline: {isOnline() ? 'Yes' : 'No'}</p>
      <p>Connection Type: {connection?.effectiveType}</p>
    </div>
  );
}

export default App;