Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 | 440x 440x 440x 440x 765x 220x 220x 440x 880x | /* eslint-disable react-hooks/exhaustive-deps */
import * as React from "react";
type ReactRef<T> = React.Ref<T> | React.MutableRefObject<T>;
export function assignRef<T = any>(ref: ReactRef<T> | undefined, value: T) {
Iif (ref == null) return;
Iif (typeof ref === "function") {
ref(value);
return;
}
try {
(ref as React.MutableRefObject<T>).current = value;
} catch (error) {
throw new Error(`Cannot assign value '${value}' to ref '${ref}'`);
}
}
/**
* React hook that merges react refs into a single memoized function
*
* @example
* import React from "react";
* import { useMergeRefs } from `@hooks/useMergeRefs`;
*
* const Component = React.forwardRef((props, ref) => {
* const internalRef = React.useRef();
* return <div {...props} ref={useMergeRefs(internalRef, ref)} />;
* });
*/
export default function useMergeRefs<T>(...refs: (ReactRef<T> | undefined)[]) {
return React.useMemo(() => {
Iif (refs.every((ref) => ref == null)) {
return null;
}
return (node: T) => {
refs.forEach((ref) => {
if (ref) assignRef(ref, node);
});
};
}, refs);
}
|