UNPKG

@wordpress/compose

Version:
46 lines 1.73 kB
import type { Ref, RefCallback } from 'react'; /** * Merges refs into one ref callback. * * It also ensures that the merged ref callbacks are only called when they * change (as a result of a `useCallback` dependency update) OR when the ref * value changes, just as React does when passing a single ref callback to the * component. * * As expected, if you pass a new function on every render, the ref callback * will be called after every render. * * If you don't wish a ref callback to be called after every render, wrap it * with `useCallback( callback, dependencies )`. When a dependency changes, the * old ref callback will be called with `null` and the new ref callback will be * called with the same value. * * Inner ref callbacks may return a cleanup function (React 19's ref callback * cleanup pattern). When a ref callback returns a function, that function is * invoked at teardown (node change, dependency change, or unmount) **instead * of** the callback being called with `null`. Callbacks that do not return a * cleanup continue to receive `null` on teardown as before. * * It's also possible to _disable_ a ref (and its behaviour) by simply not * passing the ref. * * ```jsx * const ref = useCallback( ( node ) => { * node.addEventListener( ... ); * return () => { * node.removeEventListener( ... ); * }; * }, [ ...dependencies ] ); * const otherRef = useRef(); * const mergedRefs = useMergeRefs( [ * enabled && ref, * otherRef, * ] ); * return <div ref={ mergedRefs } />; * ``` * * @param refs The refs to be merged. * @return The merged ref callback. */ export default function useMergeRefs<T>(refs: Ref<T>[]): RefCallback<T>; //# sourceMappingURL=index.d.ts.map