Created
March 29, 2019 11:04
-
-
Save SoEasy/67bd27a1d85c703a08af134dd53df537 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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