Skip to content

observable a class instance, autorun not trigger when add a new property #3801

Answered by kubk
yuwanlin asked this question in Q&A
Discussion options

You must be logged in to vote

Not sure if using a class is a good choice for dynamic keys. The purpose of a class is to have predictable keys. It will work if you switch to using an object:

class Manager {
  info = {};
  constructor() {
    makeAutoObservable(this);
    autorun(() => {
      console.log('autorun', this.info);
    });
  }
}

const m = new Manager();
m.info.name2 = 'value2';

https://stackblitz.com/edit/stackblitz-starters-87dpo9?file=src%2FApp.tsx

Replies: 2 comments 1 reply

Comment options

You must be logged in to vote
1 reply
@yuwanlin
Comment options

Answer selected by yuwanlin
Comment options

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
3 participants