Skip to content

autorun behaving differently for action vs autoAction #2676

Closed
@veesahni

Description

Given the following code:

class Store {
  a = 1;
  b = 1;

  constructor() {
    makeAutoObservable(this, {
      // incrementB: action
    });
    autorun(() => {
      console.log("autorun running");
      if (this.a > 0) {
        this.incrementB();
      }
    });
  }

  incrementA() {
    this.a += 1;
  }

  incrementB() {
    this.b += 1;
  }
}

Intended outcome:

In mobx 5.15.7, if incrementB is an @action, autorun does not observe or react to changes to B

In mobx 6.0.4, makeAutoObservable(this) should mark incrementB as an action and autorun should not observe or react to changes to B

Actual outcome:

In mobx 6.0.4, for the sample code above, mobx is observing (and reacting to) changes to B.

This means that there's a loop in the above code which results in the following error:

Reaction doesn't converge to a stable state after 100 iterations. Probably there is a cycle in the reactive function: Reaction[Autorun@11]

The solution to this is to explicitly mark incrementB as an action:

makeAutoObservable(this, {
  incrementB: action
});

There appears to be some internal differences between action and autoAction for this use case

How to reproduce the issue:

https://codesandbox.io/s/mobx-playground-forked-bfu62

Click Increment B

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions