Skip to content

Instantly share code, notes, and snippets.

@maraisr
Created November 19, 2019 23:07
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save maraisr/468774f016eae3e26a34124c277291e4 to your computer and use it in GitHub Desktop.
Save maraisr/468774f016eae3e26a34124c277291e4 to your computer and use it in GitHub Desktop.
import { MutableRefObject, useMemo } from 'react';
export const useForkedRef = <T = Element>(...refs: Array<MutableRefObject<T>>): (node: T) => void =>
useMemo(() => {
if (refs.every(ref => ref == null)) {
return null;
}
return node => {
refs.forEach(ref => {
ref.current = node;
});
};
}, refs);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment