Components

There is only one component available within this library at the moment which allows you to easily render value of Observable or Promise in your Stencil component’s template. The component is rx-async.

rx-async

rx-async is a simple component which accepts an observable or promise as a property and renders the result of the observable. It is similar to the async pipe in Angular, except it can only render stringable value (basically, anything that can be converted to string).

Properties

  • value: PromiseLike<T> | Observable<T> | T | null | undefined - value to be rendered. It can be an observable, a promise, a scalar value, or null/undefined.

  • transform: (value: T) => string - optional function to transform the value before rendering. If not provided, the value will be converted to string using String(value).

Example

Using <rx-async /> component in Stencil template
 1import {
 2    Component,
 3    ComponentInterface,
 4    Host,
 5    h,
 6}                  from '@stencil/core';
 7import { Subject } from 'rxjs';
 8
 9@Component({
10    tag: 'app-counter',
11    shadow: true,
12})
13class AppCounter implements ComponentInterface {
14
15    private readonly counter$: Subject<number> = new Subject();
16
17    public connectedCallback(): void {
18        setInterval((): void => {
19            this.counter$.next(Math.random());
20        }, 1000);
21    }
22
23    public render(): any {
24        return (
25            <Host>
26                <rx-async
27                    value={this.counter$}
28                    transform={(value: number): string => `Counter value: ${value.toFixed(2)}`}
29                />
30            </Host>
31        );
32    }
33}