Function makeAutoObservable

  • Makes existing object properties observable. Same as makeObservable but infers all the properties.

    Type Parameters

    • T

    Parameters

    • obj: T

      The object to observe.

    • Optional overrides: {
          [key: string]: "atom" | "computed" | "collection" | "reactiveProps" | false;
      } = {}

      The overrides to use to define type of the reactive property. If an override is false, the key will be ignored.

      • [key: string]: "atom" | "computed" | "collection" | "reactiveProps" | false
    • Optional options: {
          name: string;
      }

      Options to configure the observable behavior.

      • name: string

        The name of the observable object. Defaults to an empty string. Using as prefix for reactive property names.

    • Optional filter: Set<string>

      A set of property keys to selectively apply annotations.

    Returns T

    Example

    let object = {
    value: 0,
    get double() {
    return this.value * 2;
    },
    increment() {
    this.value++;
    },
    };

    makeAutoObservable(object);

    let foo = 0;

    autorun(() => {
    foo++;
    object.double;
    });
    console.log(foo); // 1

    object.increment();
    console.log(foo); // 2

    object.increment();
    console.log(foo); // 3