目录

@SyncMonitor: Synchronous Listening for Value Changes of the State Variables

@SyncMonitor is used for state management V2 to synchronously listen to the changes of state variables, enabling deep listening for state variables. It is applicable to scenarios where precise listening to deep state changes, such as changes in nested object properties and in elements in an array, is required. This addresses the issue that traditional listening methods cannot detect changes in deep-level properties, thereby improving the accuracy of state management and development efficiency.

For details, see @SyncMonitor Decorator: Synchronous Listening for Value Changes of the State Variables.

This decorator is supported since API version 23.

@SyncMonitor

const SyncMonitor: MonitorDecorator

Atomic service API: This API can be used in atomic services since API version 23.

System capability: SystemCapability.ArkUI.ArkUI.Full

Model restriction: This API can be used only in the stage model.

Type Description
MonitorDecorator @SyncMonitor decorator type.

Error codes

For details about the error codes, see State Management Error Codes.

ID Error Message
130001 The path is invalid.

Example

@Entry
@ComponentV2
struct Index {
  @Local message: string = 'Hello World';
  @Local name: string = 'Tom';

  // Use @SyncMonitor to listen for the changes of message and name at the same time.
  @SyncMonitor('message', 'name')
  onStrChange(monitor: IMonitor) {
    monitor.dirty.forEach((path: string) => {
      console.info(`${path} changed from ${monitor.value(path)?.before} to ${monitor.value(path)?.now}`);
    });
  }

  build() {
    Column() {
      Text(`message: ${this.message}`)
      Text(`name: ${this.name}`)
      Button('change string')
        .onClick(() => {
          // Modify message and trigger the onStrChange callback.
          this.message += '!';
          // Modify name and trigger the onStrChange callback.
          this.name = 'Jack';
        })
    }
  }
}