Files
simply_cpq/website/node_modules/@base-ui/utils/useValueAsRef.mjs
T
2026-08-07 21:39:30 +02:00

28 lines
765 B
JavaScript

'use client';
import { useIsoLayoutEffect } from "./useIsoLayoutEffect.mjs";
import { useRefWithInit } from "./useRefWithInit.mjs";
/**
* Untracks the provided value by turning it into a ref to remove its reactivity.
*
* Used to access the passed value inside `React.useEffect` without causing the effect to re-run when the value changes.
*/
export function useValueAsRef(value) {
const latest = useRefWithInit(createLatestRef, value).current;
latest.next = value;
// eslint-disable-next-line react-hooks/exhaustive-deps
useIsoLayoutEffect(latest.effect);
return latest;
}
function createLatestRef(value) {
const latest = {
current: value,
next: value,
effect: () => {
latest.current = latest.next;
}
};
return latest;
}