useOnline
Reactive online state. A thin wrapper around useNetwork that returns only the `isOnline Observable.
Online Status
Online
Tracks whether the browser is currently online.
You are connected to the internet.
import { const useOnline: (options?: DeepMaybeObservable<UseOnlineOptions>) => UseOnlineReturn
useOnline } from "@usels/web";
function function Component(): JSX.Element
Component() { const const isOnline$: any
isOnline$ = function useOnline(options?: DeepMaybeObservable<UseOnlineOptions>): UseOnlineReturn
useOnline();
return <JSX.IntrinsicElements.div: DetailedHTMLProps<HTMLAttributes<HTMLDivElement>, HTMLDivElement>
div>{const isOnline$: any
isOnline$.any
get() ? "Online" : "Offline"}</JSX.IntrinsicElements.div: DetailedHTMLProps<HTMLAttributes<HTMLDivElement>, HTMLDivElement>
div>;}import { createOnline } from "@usels/web";
function Component() { "use scope" const isOnline$ = createOnline();
return <div>{isOnline$.get() ? "Online" : "Offline"}</div>;}With onChange callback
Section titled “With onChange callback”Use onChange on the returned Observable to react to network status changes.
// @noErrorsimport { useMount } from "@usels/core";import { useOnline } from "@usels/web";
function Component() { const isOnline$ = useOnline();
useMount(() => { return isOnline$.onChange(({ value }) => { console.log(value ? "Back online" : "Gone offline"); }); });
return <div>{isOnline$.get() ? "Online" : "Offline"}</div>;}Parameters
Section titled “Parameters”| Parameter | Type | Description |
|---|---|---|
options | UseNetworkOptions (optional) | - |
UseOnlineOptions
Section titled “UseOnlineOptions”Returns
Section titled “Returns”UseOnlineReturn
| Name | Type | Description |
|---|---|---|
peek | { (): boolean; (): boolean; } | - |
get | (trackingType?: TrackingType | GetOptions) => boolean | - |
onChange | (cb: ListenerFn<boolean>, options?: { trackingType?: TrackingType; initial?: boolean | undefined; immediate?: boolean | undefined; noArgs?: boolean | undefined; } | undefined) => () => void | - |