Skip to content

Instantly share code, notes, and snippets.

@srph
Created May 10, 2022 13:15
Show Gist options
  • Save srph/35a2795e14ed370049a763e2aa1e769d to your computer and use it in GitHub Desktop.
Save srph/35a2795e14ed370049a763e2aa1e769d to your computer and use it in GitHub Desktop.
React: merge refs
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