Last active
August 17, 2020 12:42
-
-
Save themgoncalves/c0819eecd76fd4221a65148d12155ed3 to your computer and use it in GitHub Desktop.
Working with multiple references on React - Typescript
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
import React from 'react'; | |
type Mutable<T> = { | |
-readonly [k in keyof T]: T[k]; | |
}; | |
const mergeRefs = <T>(...refs: React.Ref<T>[]) => (value: T): void => { | |
for (let i = 0; i < refs.length; i += 1) { | |
const ref = refs[i]; | |
if (typeof ref === 'function') { | |
ref(value); | |
} else if (ref) { | |
(ref as Mutable<React.RefObject<T>>).current = value; | |
} | |
} | |
}; | |
export default mergeRefs; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment