Closed nichita-pasecinic closed 11 months ago
useObservableState
and useObservableEagerState
follow the React useState
convention. If you need to trigger rendering without changing the value, use useSubscription
+ useRef
instead.
@crimx thanks (I thought that calling .next
on subject will trigger a re-render), how would I do it with useSubscription
+ useRef
?
I'd really appreciate if you helped me.
Calling .next
will trigger the observer callback. useObservableState
and useObservableEagerState
consume the value as React state, so no re-rendering is triggered.
To force update, you can use a very simple hook like useUpdate
import { useUpdate } from "react-use";
import { useSubscription } from "observable-hooks";
const subject = new BehaviorSubject(new Map());
export const Comp = () => {
const update = useUpdate();
useSubscription(subject, update);
console.log('map keys: ', subject.keys());
}
In case a JS
Map
is used as value forBehaviorSubject
the value provided fromuseObservable*
hooks will be stale.Try: