Skip to content

Instantly share code, notes, and snippets.

@SoEasy
Created March 29, 2019 11:04
Show Gist options
  • Save SoEasy/d20537434ff32b3dc2d05203b23155c3 to your computer and use it in GitHub Desktop.
Save SoEasy/d20537434ff32b3dc2d05203b23155c3 to your computer and use it in GitHub Desktop.
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