composeRefs
Compose multiple React refs into a single ref callback. Essential when a component needs an internal ref and must also forward an external ref prop.
composeRefs merges any number of React refs into one callback. It is the low-level utility used to construct a composed ref. In component render call sites, use useComposedRefs so the callback identity stays stable across renders.
It is used internally by select, menu, tabs, radio, checkbox, navigation-list, and command-palette and is installed automatically when you add any of those components.
Installation
Before publication: Diffgazer packages are not yet published to npm, so there is no dgadd bin until you install one. Follow the canonical Copy-first mode procedure to build and pack @diffgazer/add, then install that packed tarball in the target app. The target-app install is what puts dgadd on pnpm exec.
dgadd is not public on npm yet. Until the first release, pack @diffgazer/add from the repository and install that tarball into this app, which is what puts dgadd on pnpm exec.
The hosted registry is not public yet because r.b4r7.dev does not resolve. Use this source checkout or a local registry preview until the endpoint returns 200.
Diffgazer packages are not yet published to npm. Until the first release, pack @diffgazer/ui and @diffgazer/keys from the repository and install those tarballs.
UI components require Tailwind CSS v4. Local copy mode imports src/styles/styles.css; package mode uses @diffgazer/ui CSS once packages are available.
The file is placed at lib/compose-refs.ts in your project.
The render-time recipes below use the companion hook. Install it with pnpm exec dgadd add ui/composed-refs; its registry item includes compose-refs.
API
Parameters
Returns
A single RefCallback<T> that, when called with a DOM element (or null on unmount), forwards the value to every ref in the list.
Usage
Forward an external ref while keeping an internal one
Merge a context ref with an external ref
Three refs at once
Notes
- Stable render-time refs — use
useComposedRefswhen the composed callback is passed from a component render. CallingcomposeRefsinline creates a new callback, so React detaches and reattaches it on every render. - Low-level construction — use
composeRefswhen callback identity is already stable, including inside theuseComposedRefsimplementation. - No
forwardRefneeded — both utilities support the React 19ref-as-prop pattern. - Null-safe —
nullandundefinedrefs are silently skipped. You can pass an optional consumer ref without guarding. - Order-independent — refs are called left-to-right, but all receive the same element value, so order only matters if you have side effects in a callback ref.
- No runtime dependencies — pure TypeScript, no React peer API beyond the
Reftypes.