Can array be both ref observable and deep observable at the same time? #3262
-
Suppose I have SomeClass[] array. I want some code to react to change deeply, changes in SomeClass should notify the code. I want another code to only observe changes in the array itself, array.push and assigns should update, but changes to SomeClass should be ignored. Both codes observe the same array. |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 3 replies
-
You can disable subscriptions with autorun(() => {
array.forEach(item => {
untracked(() => {
console.log(item) // item changes ignored
})
})
}) |
Beta Was this translation helpful? Give feedback.
-
To add some more context, please check out https://mobx.js.org/understanding-reactivity.html, what changes reactions depend on doesn't depend on what is observable (that is just the prerequisite), but on what is read. So if one reaction deeply reads the data, and the other one only shallowly the array, one will respond to deep changes and the other one to array changes only. There is nothing special that needs to be done to achieve that. When you do read some data in a reaction, but still don't want to react to them in the future, then |
Beta Was this translation helpful? Give feedback.
To add some more context, please check out https://mobx.js.org/understanding-reactivity.html, what changes reactions depend on doesn't depend on what is observable (that is just the prerequisite), but on what is read. So if one reaction deeply reads the data, and the other one only shallowly the array, one will respond to deep changes and the other one to array changes only. There is nothing special that needs to be done to achieve that.
When you do read some data in a reaction, but still don't want to react to them in the future, then
untracked
comes into play as suggested by @urugator, but these cases are in practice pretty rare.