Skip to content

Hooks

The small hydration-safe React hooks every app wrote its own copy of, plus cn for class names.

API

ClientOnly

PropTypeRequiredDoes
childrenReactNodeYes
fallbackReactNodeYes

Functions

FunctionSignatureDoes
useCleanId(): stringReact's useId with only letters and digits, safe in an id, a selector and an anchor (React's own has colons).
useHydrated(): booleanFalse on the server and in the render that hydrates; true after, and at once for a part first drawn in the browser.
useMedia(query: string, serverValue?: boolean): booleanWhether a media query matches; serverValue (false) on the server and while hydrating.
usePrefersReducedMotion(): booleanTrue when the reader asked for less motion. A script-driven motion checks it; CSS motion sits in its own media query.
useBrowserZone<T extends string | null>(fallback: T): string | TThe browser's time zone once hydrated; fallback (the server's) until then.
useRemembered(key: string): [string | null, (value: string | null) => void]A value remembered in this browser under key (null on the server, while hydrating, or when none is kept) and its setter (null forgets it). Every reader of the key on the page, and in other tabs, follows a change. Where storage is refused it is kept for this page only.
useNow(ms?: number, serverNow?: number): numberThe time now, in milliseconds, ticking every ms (30 s) once the page is live; 0 on the server and while hydrating, so a time measured against it says the same words in both renders. Pass the server's own read time as serverNow to draw against it until the clock starts.
useShownFor<T>(result: T | null | undefined, ms: number, clears?: (result: T) => boolean): [shown: T | undefined, hide: () => void]A result shown for ms after it arrives, then gone: "Saved", "Copied", a bulk action's count. Returns the result while it shows (undefined after) and a function that hides it now (a close button). A new result (another object) shows again. clears picks the results that go on their own; the rest (a failure, usually) stay until hidden or replaced. The hook remembers the one it hid, so a re-render does not bring it back.
useIsoLayoutEffect(effect: EffectCallback, deps?: DependencyList | undefined): voiduseLayoutEffect in the browser, useEffect on the server, where a layout effect never runs and React warns about it: for a measure or a write that must land before paint.
cn(...parts: (string | false | null | undefined)[]): stringSmall helpers every React app in the network wrote its own copy of. No framework here: the React hooks are in hooks.react.tsx. Joins class names, dropping the empty ones: cn("cap-btn", busy && "is-busy"). No merging.
clamp(n: number, lo: number, hi: number): numbern held to lo to hi; where hi is under lo, lo.
listText(words: readonly string[], join?: "and" | "or"): string"A, B, and C" (and "A and B"; "A, B, or C" with "or"): the platform's own list, in American English with its serial comma.
reducedMotion(): booleanThe reader asks for reduced motion (false where there is no window: a server, a test).
uid(prefix: string): stringA new id on every call, prefix-1, prefix-2: for a part a script gives an id to.
readStored(key: string): string | null
writeStored(key: string, value: string | null): voidStores a value (null forgets it) and tells every reader on the page.
subscribeStored(listener: () => void): () => voidCalls listener when a stored value changes here or in another tab.