All Projects → oieduardorabelo → use-navigator-online

oieduardorabelo / use-navigator-online

Licence: other
⚛️ React Hooks to detect when your browser is online/offline.

Programming Languages

typescript
32286 projects

Projects that are alternatives of or similar to use-navigator-online

File-Explorer
A File Manager with stunning design & astonishing develops, beautifully written in PHP, everything fused in a single file.
Stars: ✭ 31 (+34.78%)
Mutual labels:  online, offline
ngx-online-status
🔛 Angular 5+ Detect online/offline state
Stars: ✭ 23 (+0%)
Mutual labels:  online, offline
Data Collection Dotnet
Data collection application built using the .NET Runtime SDK.
Stars: ✭ 17 (-26.09%)
Mutual labels:  online, offline
Mono-PWA
Monobank PWA — unofficial online web client for monobank
Stars: ✭ 24 (+4.35%)
Mutual labels:  online, offline
V Offline
🔌 Simple VueJS component to detect offline & online changes.
Stars: ✭ 307 (+1234.78%)
Mutual labels:  online, offline
Vue Offline
Offline states and storage for Vue PWA
Stars: ✭ 308 (+1239.13%)
Mutual labels:  online, offline
Night
Weekly Go Online Meetup via Bilibili|Go 夜读|通过 bilibili 在线直播的方式分享 Go 相关的技术话题,每天大家在微信/telegram/Slack 上及时沟通交流编程技术话题。
Stars: ✭ 10,058 (+43630.43%)
Mutual labels:  online, offline
react-7h-hooks
(持续增加中)提供一些偏业务的实用 react hooks, 让你每天只工作 7 小时 !
Stars: ✭ 15 (-34.78%)
Mutual labels:  react-hooks
use-double-click
React hook for combining double-click function into click event, as well as repeatable double-click
Stars: ✭ 17 (-26.09%)
Mutual labels:  react-hooks
Is Reachable
Check if servers are reachable
Stars: ✭ 249 (+982.61%)
Mutual labels:  online
Z S Overview Pack
Overview Pack for EVE Online
Stars: ✭ 247 (+973.91%)
Mutual labels:  online
use-axios-react
React axios hooks for CRUD
Stars: ✭ 31 (+34.78%)
Mutual labels:  react-hooks
fetchye
✨ If you know how to use Fetch, you know how to use Fetchye [fetch-yae]. Simple React Hooks, Centralized Cache, Infinitely Extensible.
Stars: ✭ 36 (+56.52%)
Mutual labels:  react-hooks
react-hooks-lifecycle
⚛️ 🪝 ⏳ React hooks lifecycle diagram: Functional components lifecycle explained
Stars: ✭ 194 (+743.48%)
Mutual labels:  react-hooks
exactonline-api-ruby-client
A wrapper for the Exact Online API
Stars: ✭ 20 (-13.04%)
Mutual labels:  online
Cedardeploy
cedardeploy:发布系统基于python,flask,mysql,git,ssh-key,supervisor.支持多类型,上线,回滚,监控,报警
Stars: ✭ 248 (+978.26%)
Mutual labels:  online
Arrowgene.DragonsDogmaOnline
Server for Dragons Dogma Online
Stars: ✭ 87 (+278.26%)
Mutual labels:  online
eslint-config-ns
ESLint config ready to be used in multiple projects. Based on Airbnb's code style with prettier, jest and react support.
Stars: ✭ 27 (+17.39%)
Mutual labels:  react-hooks
react-facade
Experimental dependency injection for React hooks
Stars: ✭ 95 (+313.04%)
Mutual labels:  react-hooks
react-use-countdown
React hook for countdown state.
Stars: ✭ 19 (-17.39%)
Mutual labels:  react-hooks

@oieduardorabelo/use-navigator-online

React Hooks to detect when your browser is online/offline using window.navigator.onLine API.

To install it:

yarn add @oieduardorabelo/use-navigator-online

Example

An online demo is available at CodeSandbox:

If you've any issues, open an issue with a CodeSandbox link with your issue

API Explained

In your app, you can add:

import { useNavigatorOnline } from '@oieduardorabelo/use-navigator-online';

function App() {
  let details = useNavigatorOnline(options)
  ...
}

details object is composed of:

  • details.isOnline: It is a Boolean value, true when online
  • details.isOffline: It is a Boolean value, true when offline
  • details.status: Returns a default String, when online it is "online" and when offline it is "offline". You can customize it using options param

options object is composed of:

  • options.whenOnline: Can be any valid React children. It will replace the String returned in details.status when online.
  • options.whenOffline: Can be any valid React children. It will replace the String returned in details.status when offline.
  • options.startOnline: To support SSR, you can control the initial rendering mode using this option. It is a boolean value to determine which state your application should use first: true for "online-first" or false for "offline-first". Defaults to true/online-first. The value will be synced with window.navigator.onLine inside an useEffect when your application is rendered in the browser.

Examples

Using isOnline and isOffline flags:

import { useNavigatorOnline } from '@oieduardorabelo/use-navigator-online';

function App() {
  let { isOnline, isOffline } = useNavigatorOnline();

  return (
    <div>
      {isOnline && <span>We are online!</span>}
      {isOffline && <span>We are offline!</span>}
    </div>
  );
}

Using default status:

import { useNavigatorOnline } from '@oieduardorabelo/use-navigator-online';

function App() {
  // will toggle between "online" and "offline"
  let { status } = useNavigatorOnline();

  return <div>Browser now is {status}!</div>;
}

Custom values for status with whenOnline and whenOffline:

import { useNavigatorOnline } from '@oieduardorabelo/use-navigator-online';

function App() {
  // you can pass any React children in "whenOnline" and "whenOffline"
  let { status } = useNavigatorOnline({
    whenOnline: <h1>WE ARE ONLINE!</h1>,
    whenOffline: <h4>Damn, offline :(</h4>,
  });

  return <div>{status}</div>;
}

No extra configuration is needed to use it on SSR:

import { useNavigatorOnline } from '@oieduardorabelo/use-navigator-online';

function App() {
  let { status } = useNavigatorOnline();

  return <div>{status}</div>;
}

You can initialize your application offline-first. This will sync with window.navigator.onLine when your application starts, using a useEffect. Trigerring a re-render of your application.

import { useNavigatorOnline } from '@oieduardorabelo/use-navigator-online';

function App() {
  let { status } = useNavigatorOnline({
    startOnline: false
  });

  return <div>{status}</div>;
}

License

MIT License © Eduardo Rabelo

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].