victory-core
Version:
23 lines • 662 B
JavaScript
import * as Helpers from "./helpers";
/**
* Used to merge multiple React refs into a single callback ref.
*
* @example
* ```tsx
* <div ref={mergeRefs([ref, forwardedRef])} />
* ```
*/
export function mergeRefs(refs) {
return value => {
refs.forEach(ref => {
// If the ref is a function, it's a callback ref and we call it with the value.
if (Helpers.isFunction(ref)) {
ref(value);
} else if (ref !== null && ref !== undefined) {
// If the ref is an object (not null and not undefined), it's an object ref.
// We assign the value to its 'current' property.
ref.current = value;
}
});
};
}