Skip to content
Sensors

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
>;
}

Use onChange on the returned Observable to react to network status changes.

// @noErrors
import { 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>;
}
ParameterTypeDescription
optionsUseNetworkOptions (optional)-
OptionTypeDefaultDescription
windowWindowSource--

UseOnlineReturn

NameTypeDescription
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-

View on GitHub