Skip to Content
Reactant
reactant-moduleModules

core/watch

Functions

watch

▸ watch<P, T>(service, selector, watcher, options?): Unsubscribe

Description

You can use watch to observe a specific state changes in any class module.

Example

@injectable()
class Counter {
  constructor() {
    watch(this, () => this.count, (newValue) => {
      if (newValue === 3) {
        console.log(`new value: ${newValue}`);
      }
    });
  }

  @state
  count = 0;

  @action
  increase() {
    this.count += 0;
  }
}

const app = testBed({
  modules: [],
  main: Counter,
});

Type parameters

NameType
Pextends boolean
Textends any

Parameters

NameTypeDescription
serviceThisServiceModule instance
selectorSelector<P extends true ? readonly [T] | [...T[]] : T>Watched values
watcherWatcher<T>Watch callback with value changes
options?ObjectWatch options
options.isEqual?(x: unknown, y: unknown) => boolean-
options.multiple?PUse multiple values watching

Returns

Unsubscribe

Defined in

packages/reactant-module/src/interfaces.ts:187

On this page