@carbon/react
Version:
React components for the Carbon Design System
36 lines (34 loc) • 1.15 kB
JavaScript
/**
* Copyright IBM Corp. 2016, 2026
*
* This source code is licensed under the Apache-2.0 license found in the
* LICENSE file in the root directory of this source tree.
*/
require("../_virtual/_rolldown/runtime.js");
let react = require("react");
//#region src/internal/useMergedRefs.ts
/**
* Copyright IBM Corp. 2016, 2025
*
* This source code is licensed under the Apache-2.0 license found in the
* LICENSE file in the root directory of this source tree.
*/
/**
* Merges multiple refs into a single callback ref.
*
* This hook is useful when you need to attach multiple refs (for example, a ref
* passed from `forwardRef` and a local ref from `useRef`) to the same node. It
* accepts an array of refs and returns a callback ref that, when attached to a
* node, assigns that node to every ref in the array.
*/
const useMergedRefs = (refs) => {
const memoizedRefs = (0, react.useMemo)(() => refs, refs);
return (0, react.useCallback)((node) => {
memoizedRefs.forEach((ref) => {
if (typeof ref === "function") ref(node);
else if (ref) ref.current = node;
});
}, [memoizedRefs]);
};
//#endregion
exports.useMergedRefs = useMergedRefs;