I am managing the state using mobx(6.3.12) in react. But I can't observe deep changes of objects.
Here my sources.
- store
class ApiStore {
values = {
name: '',
author: '',
}
constructor() {
makeObservable(this, {
values: observable,
setValues: action
})
}
setValues(key, value) {
values[key] = value;
}
}
observe(not all work)
store = new ApiStore(); useEffect(() => { console.log(values); }, [store.values]); store = new ApiStore(); useEffect(() => { autorun(() => console.log(store.values)); }, [])state change
... <NormalTextValue onChange={(e) => store.setValues(id, e.target.value)} value={store.values[id]}/>
I realized that not being able to read deep change was the cause. Changes can be detected by accessing them via dereferencing. (ex. [store.values.name])
So I changed it to the following to get the desired result.
setValues(key, value) {
this.values[key] = value;
this.values = {...this.values};
}
Q: According to the docs, the default option of observable is known as .deep. So I thought I could automatically detect deep change of object (through this.values[key] = values). But it seems that the reference value of the object has to be changed. Could you please explain this or point out the error?