@equinor/fusion-observable
Version:
23 lines (22 loc) • 881 B
TypeScript
import type { Observable } from '../types';
/**
* React hook that keeps a `ref` synchronised with the latest value from an
* observable. The ref is updated via a layout-effect subscription so it is
* always current during commit phase reads.
*
* If the observable is a `BehaviorSubject` (or any subject with a `.value`
* property), the ref is initialised with that value.
*
* @template S - The value type.
* @param subject - The observable to track.
* @param initial - An optional initial value for the ref.
* @returns A React ref whose `.current` mirrors the latest emitted value.
*
* @example
* ```tsx
* const countRef = useObservableRef(count$);
* // countRef.current is always the latest emitted count
* ```
*/
export declare const useObservableRef: <S>(subject: Observable<S>, initial?: S) => React.RefObject<S | undefined>;
export default useObservableRef;