Created
October 4, 2021 08:45
-
-
Save gys-dev/b26690c604d1b60819c483b9a21aec87 to your computer and use it in GitHub Desktop.
merge_refs
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
export const mergeRefs = (...refs) => { | |
const filteredRefs = refs.filter(Boolean); | |
if (!filteredRefs.length) return null; | |
if (filteredRefs.length === 0) return filteredRefs[0]; | |
return inst => { | |
for (const ref of filteredRefs) { | |
if (typeof ref === 'function') { | |
ref(inst); | |
} else if (ref) { | |
ref.current = inst; | |
} | |
} | |
}; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment