Hook to subscribe to an RxJS Observable.
Returns the current value of the observable and any error.
The value will be undefined until the observable emits its first value, unless the observable is synchronous (like a BehaviorSubject).
The type of the value emitted by the observable.
The type of the error that can be emitted by the observable. Defaults to unknown.
The RxJS Observable to subscribe to.
A tuple containing:
Hook to subscribe to an RxJS Observable with pipe operations.
The type of the value emitted by the observable after piping.
The type of the error that can be emitted by the observable. Defaults to unknown.
The RxJS Observable to subscribe to.
Operator functions to pipe to the observable.
A tuple containing:
import { useObservable } from 'use-observable-hooks';
import { interval } from 'rxjs';
import { map, filter } from 'rxjs/operators';
function EvenNumbers() {
const [evenCount] = useObservable(
interval(1000),
filter((n) => n % 2 === 0),
map((n) => n * 10)
);
return <div>Even count x10: {evenCount ?? 'Waiting...'}</div>;
}
Implementation of useObservable.