useMergeRefs

Combines multiple React refs into one callback ref.

Combines multiple React refs into one callback ref.

Usage

'use client';

import { useRef, type Ref } from 'react';
import { useMergeRefs } from '@prosazhin/pbcomponents';

function Example({ forwardedRef }: { forwardedRef: Ref<HTMLDivElement> }) {
  const localRef = useRef<HTMLDivElement>(null);
  const ref = useMergeRefs(localRef, forwardedRef);
  return <div ref={ref} />;
}

Hook API

Parameters

ParameterType / variantsPurposeDefault
...refs(Ref<T> | undefined)[]Object and callback refs to combine.—

Returns

ValueType / variantsPurpose
refRefCallback<T>Combined callback ref for the element.

On this page