DEV Community

Discussion on: Using React and RxJS together

Collapse
 
kosich profile image
Kostia Palchyk

Nice article, Osman! I love the "learn by creating" approach!
and I surely love Rx in React posts 🙂

What do you think if we complete subject on unmount (just to be safe):

const useObservedValue = value => {
    const subject = React.useRef(new BehaviorSubject(value));

    React.useEffect(() => {
        subject.current.next(value);
    }, [value]);

    // complete 
    React.useEffect(() => {
        return () => subject.current.complete();
    }, []);

    return React.useMemo(() => subject.current.asObservable(), []); // < no dep, subj is always same
};
Enter fullscreen mode Exit fullscreen mode

Just today I've published an article with a bit different approach to RxJS consumption in React: a fragment that observes it's RxJS children, e.g.:

function App(){
  return <$>You're here for { timer(0, 1000) } sec</$>
}
Enter fullscreen mode Exit fullscreen mode

It subscribes and displays in-place values from it's stream(s). Would be glad to know your thoughts!

Here's a 5min post if you're interested: