Created
March 29, 2019 11:04
-
-
Save SoEasy/d20537434ff32b3dc2d05203b23155c3 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 { useEffect, useState } from 'react'; | |
import { Observable } from 'rxjs'; | |
import { distinctUntilChanged } from 'rxjs/operators'; | |
/** | |
* Компонент-аналог *ngIf - принимает на вход поток Observable<boolean>. Если в поток приходит true - рисует содержимое | |
*/ | |
export function RxIf(props: { conditionStream: Observable<boolean>, children: JSX.Element }): JSX.Element { | |
// @ts-ignore | |
const [state, setState] = useState({ active: false }); | |
useEffect(() => { | |
const subscription = props.conditionStream.pipe( | |
distinctUntilChanged() | |
).subscribe( | |
(value: boolean) => setState({ active: value }) | |
); | |
return () => { | |
subscription.unsubscribe(); | |
}; | |
}, []); | |
// @ts-ignore | |
return state.active ? props.children : null; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment