Skip to content

useMergedRef

Merges several refs into one callback ref.

useMergedRef(a, b) returns one ref to put on an element when two parts of the code each need that element: a component that keeps its own ref and also takes the caller's ref prop, or a measuring callback beside a ref that moves focus. It takes object refs, ref callbacks (their cleanup function is kept) and undefined, in any number.

Two refs on one field

A ref callback with cleanup

Measuring

Installation

npx shadcn@latest add @khaeli/use-merged-ref

Installs with

Nothing else. This item needs no other Registry item and no npm package.

Accessible name

  • The hook adds nothing to the page. A button that moves focus through a merged ref says where focus goes: Edit the key, not Focus.

Registry JSON

The item as the shadcn CLI reads it, with every file it ships: https://ui.khaeli.com/r/use-merged-ref.json