@knitui/hooks
useId
Stable id, using a provided id when present (port of Mantine's useId).
Import
import { useId } from "@knitui/hooks";Signature
export function useId(staticId?: string): stringAny component that wires accessibility relationships — a label to its input, an
error message to the field it describes, a trigger to the panel it controls —
needs an id. It also has to let the caller supply their own. useId collapses
both cases into one line: pass the caller's id through and get it back
unchanged, pass nothing and get React's generated id instead.
The reason this is a hook rather than props.id ?? useReactId() is that ??
short-circuits. Written inline, React's useId is only called when no id was
passed, so a component that is sometimes given an id and sometimes not changes
its hook order between renders. useId always calls React's hook and picks the
result afterwards, so the order is fixed.
When to use it
- Generating the id a
labelpoints at, when the component accepts an optionalidprop. - Wiring
aria-describedbyfrom a control to its own error or help text. - Linking a disclosure trigger to the region it expands via
aria-controls.
Notes
- The generated id is stable for the lifetime of the component, so it is safe in attribute wiring that must not change between renders.
- There is no platform split — it is React's
useIdwith a fallback, and behaves the same on web and native. - Passing
staticIddoes not skip the generated id; it is still created, just unused. Two instances given the samestaticIdwill collide, which is the caller's call to make.