TelegramPWA/src/hooks/useSignalRef.ts
2023-04-15 13:54:30 +02:00

19 lines
442 B
TypeScript

import { useRef } from '../lib/teact/teact';
import type { Signal } from '../util/signals';
import useEffectOnce from './useEffectOnce';
// Allows to use signal value as "silent" dependency in hooks (not causing updates)
export function useSignalRef<T>(getValue: Signal<T>) {
const ref = useRef<T>(getValue());
useEffectOnce(() => {
return getValue.subscribe(() => {
ref.current = getValue();
});
});
return ref;
}