cosmos-explorer/src/hooks/useObservable.ts

14 lines
425 B
TypeScript
Raw Normal View History

2021-04-19 21:11:48 +01:00
import { Observable } from "knockout";
import { useEffect, useState } from "react";
export function useObservable<T>(observable: Pick<Observable<T>, "subscribe" | "peek">): T {
const [, setValue] = useState(0);
useEffect(() => {
const subscription = observable.subscribe(() => setValue((n) => 1 + n), undefined, "change");
return () => subscription.dispose();
}, [observable]);
return observable.peek();
}