mirror of
https://github.com/zoriya/v10.git
synced 2026-08-11 00:19:28 +00:00
20 lines
497 B
TypeScript
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 }>;
|
|
}
|