Created
May 10, 2022 13:15
-
-
Save srph/35a2795e14ed370049a763e2aa1e769d to your computer and use it in GitHub Desktop.
React: merge refs
This file contains hidden or 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
export function mergeRefs<T>(...refs: Ref<T>[]) { | |
return (value: T) => { | |
refs.forEach((ref) => { | |
if (typeof ref === 'function') { | |
ref(value) | |
} else if (ref.current) { | |
ref.current = value | |
} | |
}) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment