Skip to content

Instantly share code, notes, and snippets.

@SoEasy
Created March 29, 2019 11:04
Show Gist options
  • Save SoEasy/67bd27a1d85c703a08af134dd53df537 to your computer and use it in GitHub Desktop.
Save SoEasy/67bd27a1d85c703a08af134dd53df537 to your computer and use it in GitHub Desktop.
import React, { useEffect, useState } from 'react';
import { Observable } from 'rxjs';
import { distinctUntilChanged } from 'rxjs/operators';
export type RxBindProps<T> = {
stream: Observable<T>;
children(dataFromStream: T | void): JSX.Element | Array<JSX.Element> | null;
};
export function RxBind<T>(props: RxBindProps<T>): JSX.Element {
const [streamValue, setStreamValue] = useState<T | void>(void 0);
useEffect(() => {
const subscription = props.stream.pipe(
distinctUntilChanged()
).subscribe(setStreamValue);
return () => {
subscription.unsubscribe();
};
}, []);
// @ts-ignore
return props.children(streamValue);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment