Peeyush Kushwaha
Peeyush Kushwaha

Reputation: 3623

mobx `makeAutoObservable` and inheritence

I have a base class B which is a mobx store and a derived class A which is not a mobx store.

As an example, it looks something like

class B {
  name = ""
  get firstName () {
    return this.name.split(" ")[0]
  }
  constructor() {
    makeAutoObservable(this)
  }
}

class A extends B {
  // some other fields and methods
  constructor() {
    super(this)
  }
}

When I run the code new A() I get this error:

Error: [MobX] 'makeAutoObservable' can only be used for classes that don't have a superclass

This is because mobx correctly complains that it cannot automatically enumerate fields/methods of A in an object which is in a derived class B which doesn't have them as own properties, but they are up the prototypes chain instead.

But still, because I know that I want mobx to automatically go through class A's property (i.e. only one step up the prototype chain), I think that there should be a way for me to tell mobx that.

Is there a way?

Upvotes: 4

Views: 5155

Answers (1)

DeltaTango
DeltaTango

Reputation: 891

See this post:

Is it possible to create inheritance between two mobx stores?

You have to do makeObservable and it should be in the later store.

Upvotes: 4

Related Questions