Skip to content

Instantly share code, notes, and snippets.

@MarekZeman91
Created April 18, 2022 00:09
Show Gist options
  • Save MarekZeman91/6274b42de952de15824790b45eca5dd4 to your computer and use it in GitHub Desktop.
Save MarekZeman91/6274b42de952de15824790b45eca5dd4 to your computer and use it in GitHub Desktop.
import { MutableRefObject, RefCallback, RefObject } from 'react';
export const setRef = <T>(...refs: (RefCallback<T> | RefObject<T> | MutableRefObject<T> | undefined | null)[]) => {
return (instance: T | null) => {
for (const ref of refs) {
if (!ref) continue;
if (typeof ref === 'function') {
ref(instance);
}
if ('current' in ref) {
(ref as MutableRefObject<T | null>).current = instance;
}
}
};
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment