Skip to content

Instantly share code, notes, and snippets.

@gys-dev
Created October 4, 2021 08:45
Show Gist options
  • Save gys-dev/b26690c604d1b60819c483b9a21aec87 to your computer and use it in GitHub Desktop.
Save gys-dev/b26690c604d1b60819c483b9a21aec87 to your computer and use it in GitHub Desktop.
merge_refs
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