Hook to use an RxJS BehaviorSubject.
Returns the current value, a function to emit a new value, and any error.
A BehaviorSubject always has a current value and emits it to new subscribers.
The type of the value emitted by the subject.
The type of the error that can be emitted by the subject. Defaults to unknown.
The initial value for the BehaviorSubject.
A tuple containing:
Hook to use an RxJS BehaviorSubject with pipe operations.
The type of the value emitted by the subject after piping.
The type of the error that can be emitted by the subject. Defaults to unknown.
The initial value for the BehaviorSubject.
Operator functions to pipe to the subject.
A tuple containing:
import { useBehaviorSubject } from 'use-observable-hooks';
import { map } from 'rxjs/operators';
function DoubleCounter() {
const [doubledCount, setCount] = useBehaviorSubject(
0,
map((n) => n * 2)
);
return (
<button onClick={() => setCount(doubledCount / 2 + 1)}>
Doubled Count: {doubledCount}
</button>
);
}
Implementation of useBehaviorSubject.