Files
med-notes/.pnpm-store/v10/files/45/2f4a7fba83f26e95c10e8d44205280e07ceec7922b533d9466247ec11386458053b20c9166de2a989044387955fcea2157a038d7fd919fdf668b5ba6829ded
2025-05-09 05:30:08 +02:00

24 lines
412 B
Plaintext

'use client';
import { useRef } from 'react';
import { isDefined } from '../utils.js';
export default function useCachedValue<T>(getter: () => T): () => T {
const ref = useRef<T | undefined>(undefined);
const currentValue = ref.current;
if (isDefined(currentValue)) {
return () => currentValue;
}
return () => {
const value = getter();
ref.current = value;
return value;
};
}