-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Watch for nested object/array changes #1207
Comments
If you need diffs, you have to manually traverse the tree and use If you only need to react to new values, then make autorun(() => {
console.log(observablePages[1].childs[0].id);
// re-runs when observablePages array is modified or when childs prop is modified or childs array is modified or when id prop is modified
// for more info see https://mobx.js.org/best/react.html
}) |
If you want to keep Vue-ish styles of code, you're also able to do it like this. const { observable, reaction, toJS } = mobx;
const pages = [
// your nested object/array goes here
];
// make it deep observable
const oPages = observable(pages);
// watch all changes
reaction(
() => toJS(oPages),
newVal => { console.log(newVal); },
)
oPages[1].childs[0].childs.push({ id: 212, childs: [] }); // logged newVal But I don't recommend this because this is not good for performance... |
@leader22 instead of |
@urugator Oh, exactly! I've just edit my comments. Thx! 😸 |
@leader22 @urugator
will not watch for all changes in the tree @leader22 This is exactly what i need but as you said it is not good for performance if the tree is big with lot of deeply nested children So I guess currently mobx has no proper way to watch for deep changes in a tree object like Vue Could it be implemented in the future version of mobx? |
I am not familiar with autorun(() => {
console.log(Mobx.toJS(pages));
}) could be pretty close to
Why? If I understand right the subscription in vue ( |
This is Vue watch api for reference https://vuejs.org/v2/api/#vm-watch |
Aside |
@urugator make sense to me now. Thanks a lot for your explanation |
I have this nested object:
So each page will have multiple deeply nested childs. In Vue i can just use this code to watch for any changes in the tree:
How can i do that in mobx?
Thanks
The text was updated successfully, but these errors were encountered: