@etsoo/react
Version:
TypeScript ReactJs UI Independent Framework
26 lines (23 loc) • 547 B
text/typescript
/**
* Combined refs
* @param refs Refs
* @returns Callback
*/
export function useCombinedRefs<T>(...refs: (React.Ref<T> | undefined)[]) {
return (target: T | null) => {
// Ignore null reference
if (target == null) return;
// Update all refs
refs.forEach((ref) => {
// Null ref
if (!ref) return;
// Callback function
if (typeof ref === "function") {
ref(target);
} else {
// as any to update readonly property
Reflect.set(ref, "current", target);
}
});
};
}