When to call makeAutoObservable? #2831
-
Hello! But in my setup doing so does not work as expected. My app setup was bootstrapped with "dependencies": {
"mobx": "^6.0.1",
"mobx-react-lite": "^3.0.1",
"react": "^17.0.1",
"react-dom": "^17.0.1",
"react-scripts": "4.0.0",
...
} Here's a codesandbox with the issue reproduced. Since this is the first time here, and new to Mobx, I'm not sure if this is an issue or if I'm just overlooking something. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 4 replies
-
class Doubler {
constructor(val) {
this.value = val || 0;
makeAutoObservable(this);
}
} You can use property initializer, but you have to assign some initial value: class Doubler {
value = 0
constructor(val) {
makeAutoObservable(this);
}
} Because: class Doubler {
value;
constructor(val) {
console.log(this.hasOwnProperty('value')) // false
makeAutoObservable(this);
}
} |
Beta Was this translation helpful? Give feedback.
makeAutoObservable
/makeObservable
makes observable only existing properties:You can use property initializer, but you have to assign some initial value:
Because: