API

Everything @k8ordo/effects and @k8ordo/effects/testing export: the hooks, the functions and the types.

On this page

useOnline

Import from @k8ordo/effects

Whether the browser has a network connection, as navigator.onLine says, following its online and offline events.

ts
useOnline(): boolean

Returns

boolean — The value of navigator.onLine, and true on the server and while hydrating.

useFullscreen

Import from @k8ordo/effects

Whether any element is fullscreen, and a function that puts the whole page fullscreen or leaves.

ts
useFullscreen(): [boolean, (fullscreen: boolean) => Promise<void>]

Returns

[boolean, (fullscreen: boolean) => Promise<void>] — First, whether any element is fullscreen, false on the server and while hydrating. Second, a function that puts <html> fullscreen with true and leaves with false.

Caveats

  • The function returns the browser’s promise, which rejects when the browser refuses, as it does outside a click or a key press.
  • true while <html> is already fullscreen, and false while nothing is, do nothing.

useScrollDirection

Import from @k8ordo/effects

Which way the page last scrolled vertically.

ts
useScrollDirection(
  options?: ScrollDirectionOptions,
): ScrollDirection | null

Parameters

options.thresholdnumber = 0
Scrolling down within this many CSS pixels of the top leaves the direction as it was.

Returns

ScrollDirection | null — 'up' or 'down'. null until the page has scrolled, and on the server and while hydrating.

Caveats

  • Every component that asks with the same threshold reads one record.
  • A negative or non-finite threshold throws an error starting with useScrollDirection: threshold must be a non-negative number of pixels.

useWindowEvent

Import from @k8ordo/effects

Calls handler with each type event window receives while the component is mounted.

ts
useWindowEvent<K extends keyof WindowEventMap>(
  type: K | null,
  handler: (event: WindowEventMap[K]) => void,
): void

Parameters

type(K extends keyof WindowEventMap) | null
The kind of event. When it changes, the listener is added again; null listens to nothing.
handler(event: WindowEventMap[K]) => void
Called with each event, the latest render’s handler each time.

Caveats

  • Not for an element’s own events: use its on* prop, or a ref callback.

useDocumentEvent

Import from @k8ordo/effects

Calls handler with each type event document receives while the component is mounted.

ts
useDocumentEvent<K extends keyof DocumentEventMap>(
  type: K | null,
  handler: (event: DocumentEventMap[K]) => void,
): void

Parameters

type(K extends keyof DocumentEventMap) | null
The kind of event. When it changes, the listener is added again; null listens to nothing.
handler(event: DocumentEventMap[K]) => void
Called with each event, the latest render’s handler each time.

Caveats

  • Not for an element’s own events: use its on* prop, or a ref callback.

useBroadcastChannel

Import from @k8ordo/effects

Joins the BroadcastChannel called name while the component is mounted.

ts
useBroadcastChannel<T>(
  name: string | null,
  options: BroadcastChannelOptions<T>,
): (message: T) => void

Parameters

namestring | null
The channel’s name. null joins nothing.
options.parse(data: unknown) => T | undefined
Turns what arrived into a message, or returns undefined to drop it. Required.
options.onMessage((message: T) => void) | undefined
Called with each message parse accepts, the latest render’s each time.

Returns

(message: T) => void — A function that posts to the other tabs. While joined to nothing, it sends nothing.

Caveats

  • A tab never receives its own messages.

useInterval

Import from @k8ordo/effects

Calls callback every ms milliseconds while the component is mounted.

ts
useInterval(ms: number | null, callback: () => void): void

Parameters

msnumber | null
The interval in milliseconds. null stops it, and a new value starts the count again.
callback() => void
What to call, the latest render’s each time.

Caveats

  • A negative, infinite or NaN ms throws an error starting with useInterval: ms must be a non-negative number of milliseconds or null.

useTimeout

Import from @k8ordo/effects

Calls callback once, ms milliseconds after the component mounts.

ts
useTimeout(
  ms: number | null,
  callback: () => void,
): () => void

Parameters

msnumber | null
The wait in milliseconds. null cancels it.
callback() => void
What to call, the latest render’s each time.

Returns

() => void — restart, which starts the wait again from now and re-renders the component.

Caveats

  • It is not called if the component unmounts first.
  • A negative, infinite or NaN ms throws an error starting with useTimeout:.

useAnimationFrame

Import from @k8ordo/effects

Calls callback before every frame the browser paints while the component is mounted.

ts
useAnimationFrame(
  running: boolean,
  callback: (time: DOMHighResTimeStamp) => void,
): void

Parameters

runningboolean
Whether to call it. false stops it.
callback(time: DOMHighResTimeStamp) => void
What to call, with the frame’s timestamp. The latest render’s each time.

usePolling

Import from @k8ordo/effects

Calls poll once the component mounts, then again as many milliseconds later as it resolves to, until it resolves to null.

ts
usePolling<K extends PollingKey>(
  key: K | null,
  poll: (key: K, signal: AbortSignal) => Promise<number | null>,
): void

Parameters

keyK | null
What polling restarts on. A new key aborts the call in flight and starts again, and null stops polling.
poll(key: K, signal: AbortSignal) => Promise<number | null>
Asks once, and resolves to the milliseconds until the next call, or null to stop. The latest render’s each time.

Caveats

  • A rejected poll, or one that resolves to a negative or non-finite number, stops polling, and the component fails with the error in its next render.
  • signal is aborted when the key changes and when the component unmounts.

defineResource

Import from @k8ordo/effects

Defines a value that loads in the browser. Each key loads once and is kept for the life of the page.

ts
defineResource<K extends ResourceKey, T>(
  options: ResourceOptions<K, T>,
): Resource<K, T>

Parameters

options.load(key: K) => Promise<T>
Loads the value for a key. It runs in the browser, during a render.

Returns

Resource<K, T> — The resource to pass to useResource.

Caveats

  • It is not a hook, and its module has no 'use client', so a Server Component’s module can import a definition too.
  • load only reads. It never calls a Server Function.

useResource

Import from @k8ordo/effects

Reads resource for key with use(). The component suspends until the value arrives, and a rejection goes to the nearest error boundary.

ts
useResource<K extends ResourceKey, T>(
  resource: Resource<K, T>,
  key: K,
): T

Parameters

resourceResource<K, T>
A resource from defineResource.
keyK
The key to read. Keys that write the same JSON read one entry.

Returns

T — The loaded value.

Caveats

  • It needs a <Suspense> above it. On the server, the nearest one’s fallback is rendered.
  • To keep the old value on screen while a new key loads, pass a key from useDeferredValue, or change it inside startTransition.

resetResources

Import from @k8ordo/effects/testing

Empties every resource’s cache, so the next read of any key loads it again.

ts
resetResources(): void

Caveats

  • Call it between tests that read the same resource.

BroadcastChannelOptions

Import from @k8ordo/effects

The type of useBroadcastChannel’s second argument.

ts
type BroadcastChannelOptions<T> = {
  readonly parse: (data: unknown) => T | undefined;
  readonly onMessage?: (message: T) => void;
};

PollingKey

Import from @k8ordo/effects

The type of usePolling’s key. It is compared with Object.is, so only primitives are accepted.

ts
type PollingKey = string | number | boolean;

Resource

Import from @k8ordo/effects

The type of the resource defineResource returns.

ts
type Resource<K extends ResourceKey, T> = {
  readonly refresh: (key: K) => void;
};

Fields

refresh(key: K) => void
Loads the key again and re-renders the components reading it. Inside startTransition they keep the current value on screen until the new one arrives. A key nobody reads yet starts loading.

ResourceKey

Import from @k8ordo/effects

The type of a resource’s key: anything JSON can write.

ts
type ResourceKey =
  | string
  | number
  | boolean
  | null
  | readonly ResourceKey[]
  | { readonly [field: string]: ResourceKey };

ResourceOptions

Import from @k8ordo/effects

The type of defineResource’s argument.

ts
type ResourceOptions<K extends ResourceKey, T> = {
  readonly load: (key: K) => Promise<T>;
};

ScrollDirection

Import from @k8ordo/effects

The type of the direction useScrollDirection returns.

ts
type ScrollDirection = 'up' | 'down';

ScrollDirectionOptions

Import from @k8ordo/effects

The type of useScrollDirection’s argument.

ts
type ScrollDirectionOptions = {
  readonly threshold?: number;
};
k8ordo

React libraries that use Baseline features without holding back.