use-network-api
v2.0.1
Published
Use Network API with React hooks
Downloads
6
Readme
use-network-api
A simple JavaScript utility to access the network api.
# 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;