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.
useOnline(): booleanReturns
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.
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.
truewhile<html>is already fullscreen, andfalsewhile nothing is, do nothing.
useScrollDirection
Import from @k8ordo/effects
Which way the page last scrolled vertically.
useScrollDirection(
options?: ScrollDirectionOptions,
): ScrollDirection | nullParameters
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
thresholdreads one record. - A negative or non-finite
thresholdthrows an error starting withuseScrollDirection: 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.
useWindowEvent<K extends keyof WindowEventMap>(
type: K | null,
handler: (event: WindowEventMap[K]) => void,
): voidParameters
type(K extends keyof WindowEventMap) | null- The kind of event. When it changes, the listener is added again;
nulllistens 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.
useDocumentEvent<K extends keyof DocumentEventMap>(
type: K | null,
handler: (event: DocumentEventMap[K]) => void,
): voidParameters
type(K extends keyof DocumentEventMap) | null- The kind of event. When it changes, the listener is added again;
nulllistens 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.
useBroadcastChannel<T>(
name: string | null,
options: BroadcastChannelOptions<T>,
): (message: T) => voidParameters
namestring | null- The channel’s name.
nulljoins nothing. options.parse(data: unknown) => T | undefined- Turns what arrived into a message, or returns
undefinedto drop it. Required. options.onMessage((message: T) => void) | undefined- Called with each message
parseaccepts, 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.
useInterval(ms: number | null, callback: () => void): voidParameters
msnumber | null- The interval in milliseconds.
nullstops 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
NaNmsthrows an error starting withuseInterval: 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.
useTimeout(
ms: number | null,
callback: () => void,
): () => voidParameters
msnumber | null- The wait in milliseconds.
nullcancels 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
NaNmsthrows an error starting withuseTimeout:.
useAnimationFrame
Import from @k8ordo/effects
Calls callback before every frame the browser paints while the component is mounted.
useAnimationFrame(
running: boolean,
callback: (time: DOMHighResTimeStamp) => void,
): voidParameters
runningboolean- Whether to call it.
falsestops 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.
usePolling<K extends PollingKey>(
key: K | null,
poll: (key: K, signal: AbortSignal) => Promise<number | null>,
): voidParameters
keyK | null- What polling restarts on. A new key aborts the call in flight and starts again, and
nullstops polling. poll(key: K, signal: AbortSignal) => Promise<number | null>- Asks once, and resolves to the milliseconds until the next call, or
nullto 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. signalis 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.
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. loadonly 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.
useResource<K extends ResourceKey, T>(
resource: Resource<K, T>,
key: K,
): TParameters
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 insidestartTransition.
resetResources
Import from @k8ordo/effects/testing
Empties every resource’s cache, so the next read of any key loads it again.
resetResources(): voidCaveats
- Call it between tests that read the same resource.
BroadcastChannelOptions
Import from @k8ordo/effects
The type of useBroadcastChannel’s second argument.
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.
type PollingKey = string | number | boolean;Resource
Import from @k8ordo/effects
The type of the resource defineResource returns.
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
startTransitionthey 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.
type ResourceKey =
| string
| number
| boolean
| null
| readonly ResourceKey[]
| { readonly [field: string]: ResourceKey };ResourceOptions
Import from @k8ordo/effects
The type of defineResource’s argument.
type ResourceOptions<K extends ResourceKey, T> = {
readonly load: (key: K) => Promise<T>;
};ScrollDirection
Import from @k8ordo/effects
The type of the direction useScrollDirection returns.
type ScrollDirection = 'up' | 'down';ScrollDirectionOptions
Import from @k8ordo/effects
The type of useScrollDirection’s argument.
type ScrollDirectionOptions = {
readonly threshold?: number;
};