fp/services/next/app/lib/shareRef.ts

17 lines
675 B
TypeScript
Raw Normal View History

2024-01-20 16:16:14 +00:00
import type { MutableRefObject, RefCallback } from 'react';
type RefType<T> = MutableRefObject<T> | RefCallback<T> | null;
export const shareRef = <T>(refA: RefType<T>, refB: RefType<T>): RefCallback<T> => instance => {
if (typeof refA === 'function') {
refA(instance);
} else if (refA && 'current' in refA) {
(refA as MutableRefObject<T>).current = instance as T; // Use type assertion to tell TypeScript the type
}
if (typeof refB === 'function') {
refB(instance);
} else if (refB && 'current' in refB) {
(refB as MutableRefObject<T>).current = instance as T; // Use type assertion to tell TypeScript the type
}
};