Files
v10/packages/react/src/utils/use-latest-ref.ts
T

20 lines
497 B
TypeScript

'use client';
import { useRef } from 'react';
/**
* Keep a ref that always points to the latest value.
*
* Useful for capturing callbacks or derived values inside closures
* that are created once (e.g. factory callbacks) without stale reads.
*/
export function useLatestRef<Value>(value: Value): Readonly<{ current: Value }> {
const ref = useRef(value);
ref.current = value;
return ref;
}
export namespace useLatestRef {
export type Result<Value> = Readonly<{ current: Value }>;
}