import { useCallback } from 'react'; export function useMergedRefs(...refs: React.Ref[]) { const setRef: React.RefCallback = useCallback( (node: T | null) => { const cleanups: (() => void)[] = []; for (const ref of refs) { if (typeof ref === 'function') { const cleanup = ref(node); if (cleanup) { cleanups.push(cleanup); } } else if (ref) { // eslint-disable-next-line react-hooks/immutability -- Refs can be mutated ref.current = node; } } return () => { for (const cleanup of cleanups) { cleanup(); } }; }, [refs], ); // eslint-disable-next-line react-hooks/immutability return setRef; }