observable a class instance, autorun not trigger when add a new property #3801
-
this is my code:
info is a class instance, I want to add dynamic property on it, but autorun not triggered. https://stackblitz.com/edit/stackblitz-starters-qr1p6q?file=src%2FApp.tsx |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 1 reply
-
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 |
Beta Was this translation helpful? Give feedback.
-
Won't work see: https://mobx.js.org/observable-state.html#non-convertibles
For observables created with |
Beta Was this translation helpful? Give feedback.
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:
https://stackblitz.com/edit/stackblitz-starters-87dpo9?file=src%2FApp.tsx