Skip to content
Discussion options

You must be logged in to vote

Yes, makeObservable and makeAutoObservable both accept any object or class instance at any point after creation. You don't have to call them inside the constructor.

Here's an example with a pure domain class made reactive after creation:

import { makeAutoObservable } from 'mobx';

class UserModel {
  name = 'Alice';
  age = 30;

  get isAdult() {
    return this.age >= 18;
  }

  setAge(newAge: number) {
    this.age = newAge;
  }
}

// 1. Plain domain instance without MobX dependencies
const user = new UserModel();

// 2. Make the existing instance reactive
makeAutoObservable(user);

Keep in mind

  • Call it only once per instance: Calling makeObservable or makeAutoObservable twice on the sa…

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by mweststrate
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants
⌂ 换网址