mergeRefs.tsx 456 B

12345678910111213141516
  1. /**
  2. * Combine refs to allow assignment to all passed refs
  3. */
  4. export default function mergeRefs<T = any>(
  5. refs: Array<React.MutableRefObject<T> | React.LegacyRef<T> | undefined>
  6. ): React.RefCallback<T> {
  7. return value => {
  8. refs.forEach(ref => {
  9. if (typeof ref === 'function') {
  10. ref(value);
  11. } else if (ref !== null && ref !== undefined) {
  12. (ref as React.MutableRefObject<T | null>).current = value;
  13. }
  14. });
  15. };
  16. }