Created
April 18, 2022 00:09
-
-
Save MarekZeman91/6274b42de952de15824790b45eca5dd4 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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