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 | 9x 41x 41x 40x 41x 22x 12x | import { useEffect, useMemo, useRef, type SetStateAction } from "react";
/**
* Type guard for the functional-updater form of a `SetStateAction`.
*
* `setValue(prev => next)` vs `setValue(next)` — this narrows the union so the
* updater can be called with the previous value.
*/
export function isUpdater<T>(
value: SetStateAction<T>
): value is (prev: T) => T {
return typeof value === "function";
}
/**
* Returns a stable function that always invokes the *latest* `callback`.
*
* This lets a hook expose a setter/handler with a permanent identity (safe as an
* effect dependency) while still calling the freshest `onChange` the consumer
* passed — without re-subscribing listeners on every render. Mirrors the
* "callback ref" pattern used by Radix UI and `useEventCallback`.
*
* The stored ref is updated in an effect (not during render) so it stays
* concurrent-mode / StrictMode safe.
*/
export function useCallbackRef<Args extends unknown[], Return>(
callback: ((...args: Args) => Return) | undefined
): (...args: Args) => Return | undefined {
const callbackRef = useRef(callback);
useEffect(() => {
callbackRef.current = callback;
});
return useMemo(
() =>
(...args: Args) =>
callbackRef.current?.(...args),
[]
);
}
|