viewChildObservable()

Observable creation operator

Observes element in the DOM subtree of the component view that match the given selector. Inspired by Angular’s @ViewChild decorator, this operator allows you to observe changes in the view child element of a component.

Uses renderObservable() to observe rendering of the component and emits the current view child element whenever the component is rendered.

When initialized, the observable will emit the current content child element (or null if it does not exist).

Implementation is more performant when component uses shadow DOM, while for emulated shadow DOM, the observable will filter out elements that are not part of the emulated view child.

Note

This operator is alternative to JSX’s ref attribute. With this operator, you can observe the view child element as a stream of changes which can be useful in reactive programming scenarios.

Warning

Observer is very efficient when component uses shadow DOM, while for emulated shadow DOM, the observable will filter out elements that are not part of the emulated view child. Filtering process in that case might be expensive if there are many elements in the DOM subtree of the component.

viewChildObservable(cmp, selector, options)

Creates an observable that emits the current view child element of a Stencil component whenever the component is rendered.

viewChildObservable<E extends Element = Element>(cmp: ComponentInterface, selector: string, options?: ViewChildObservableOptions): Observable<E | null>
Arguments:
  • cmp (ComponentInterface) – The Stencil component instance for which to observe the view child element.

  • selector (string) – The CSS selector used to identify the view child element.

  • options (ViewChildObservableOptions) – Optional configuration options for the observable.

Returns:

Observable that emits the current view child element whenever the component is rendered.

Return type:

Observable<E | null>

class ViewChildObservableOptions()

Configuration options for observing a view child element.

export type ViewChildObservableOptions = {
    distinct?: ElementComparatorFn | boolean;
};
ViewChildObservableOptions.distinct
Type:

ElementComparatorFn | boolean

Optional: Yes

Default: true

Controls whether the observable emits only when the observed element changes.

  • true - emit only when the element reference changes.

  • false - emit every time, regardless if element reference changes.

  • ElementComparatorFn - use a custom comparator to determine whether two elements are equal.

The comparator should return true when elements are equal and false otherwise.

Example

Observe view child element changes
 1import {
 2    Component,
 3    ComponentInterface,
 4    h,
 5} from '@stencil/core';
 6import { viewChildObservable } from '@runopenode/rxjs-stencil';
 7
 8@Component({
 9    tag: 'my-cmp',
10    shadow: true,
11})
12class MyCmp implements ComponentInterface {
13
14    public constructor() {
15         viewChildObservable(this, '.my-view-child')
16             .subscribe((element: Element | null): void => {
17                 console.log('View child element changed:', element);
18             });
19    }
20
21    public render(): JSX.Element {
22        return (
23            <div>
24                <div class="my-view-child">Hello, World!</div>
25            </div>
26        );
27    }
28}