Skip to content

unwatch

Annotation for unwatching changes to a property and a getter

When used as an annotation:

  • Combine with @observable or @nested (and their variants) to stop watching changes.
    @computed needs none, as it is not watched unless @watch is specified.
  • You cannot re-enable watching once @unwatch is specified.

When used as a function:

  • Runs a piece of code without changes being detected by Watcher.
    unwatch(() => (model.field = "value"));
  • Only what the function itself changes goes undetected. Watching resumes as soon as it returns, so the changes
    made around it are detected as usual, even when all of it happens in one transaction.
    runInAction(() => {
    model.field1 = "value"; // Detected
    unwatch(() => {
    model.field2 = "value"; // Not detected
    });
    model.field3 = "value"; // Detected
    });
  • Limitation: a watcher processes changes in batches, where all it can tell is when a key first changed in the
    batch. A key that the function changes therefore goes undetected until the next batch — the rest of the
    transaction, or longer under a custom reactionScheduler. A key changed before the function and again inside
    it is detected.

unwatch(action): void

Defined in: watcher.ts:181

() => void

void

unwatch(target, propertyKey): void

Defined in: watcher.ts:181

object

string | symbol

void

unwatch(value, context): void | ((this: object) => any)

Defined in: watcher.ts:181

(this: object) => any

ClassGetterDecoratorContext

void | ((this: object) => any)

unwatch(value, context): void | ClassAccessorDecoratorResult<object, any>

Defined in: watcher.ts:181

ClassAccessorDecoratorTarget<object, any>

ClassAccessorDecoratorContext

void | ClassAccessorDecoratorResult<object, any>

unwatch(value, context): any

Defined in: watcher.ts:181

undefined

ClassFieldDecoratorContext<object, any>

any