Skip to content

Instantly share code, notes, and snippets.

@simontreny
Last active October 18, 2019 13:13
Show Gist options
  • Save simontreny/32a74c621c92ec40e87e24b9a9d67b12 to your computer and use it in GitHub Desktop.
Save simontreny/32a74c621c92ec40e87e24b9a9d67b12 to your computer and use it in GitHub Desktop.
import { useEffect, useState } from "react";
import { Observable } from "./observable";
export function useObservable<T>(observable: Observable<T>): T {
const [val, setVal] = useState(observable.get());
useEffect(() => {
return observable.subscribe(setVal);
}, [observable]);
return val;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment