@primer/react
Version:
An implementation of GitHub's Primer Design System using React
37 lines (34 loc) • 1.04 kB
JavaScript
import { c } from 'react-compiler-runtime';
import { useImperativeHandle } from 'react';
/**
* Use a ref object as the imperative handle for a forwarded ref. This can be used to
* synchronize the ref object with the forwarded ref and allow local access the reference
* instance with `.current`.
*
* **NOTE**: The `refObject` should be passed to the underlying element, NOT the `forwardedRef`.
*
* @deprecated Migrate to `useMergedRefs`. It's safer, faster, and easier to use:
*
* ```diff
* const ref = useRef(null)
*
* - useRefObjectAsForwardedRef(forwardedRef, ref)
* + const mergedRef = useMergedRefs(forwardedRef, ref)
*
* - return <div ref={ref} />
* + return <div ref={mergedRef} />
* ```
*/
function useRefObjectAsForwardedRef(forwardedRef, refObject) {
const $ = c(2);
let t0;
if ($[0] !== refObject.current) {
t0 = () => refObject.current;
$[0] = refObject.current;
$[1] = t0;
} else {
t0 = $[1];
}
useImperativeHandle(forwardedRef, t0);
}
export { useRefObjectAsForwardedRef };