How does computed tracks observable access in functions called inside it? #3838
Replies: 1 comment 1 reply
-
Good starts: https://mobx.js.org/understanding-reactivity.html /
https://hackernoon.com/becoming-fully-reactive-an-in-depth-explanation-of-mobservable-55995262a254
…On Thu, Feb 29, 2024 at 3:41 PM Attila Greguss ***@***.***> wrote:
Basically, why does this work?
import { autorun, computed, makeObservable, observable, runInAction, trace } from "mobx";
function Test(vm: VM) {
const hasDevices = vm.devices != null && vm.devices.length > 0;
return {
hasDevices: hasDevices,
}
}
class VM {
@observable
devices: number[] | null = null;
@computed
get TestInfo() {
const test = Test(this);
trace();
return test;
}
constructor() {
makeObservable(this);
}
}
const vm = new VM();
autorun(() => {
console.log(vm.TestInfo.hasDevices)
})
runInAction(() => vm.devices = [1, 2, 3]);
// true
runInAction(() => vm.devices = null);
// false
runInAction(() => vm.devices = [1, 2, 3]);
// true
The output is:
[mobx.trace] ***@***.***' tracing enabled
false
[mobx.trace] ***@***.***' is invalidated due to a change in: ***@***.***'
true
[mobx.trace] ***@***.***' is invalidated due to a change in: ***@***.***'
false
[mobx.trace] ***@***.***' is invalidated due to a change in: ***@***.***'
true
—
Reply to this email directly, view it on GitHub
<#3838>, or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AAN4NBHLKPFC4BMHPHNUSFLYV463NAVCNFSM6AAAAABEACER2SVHI2DSMVQWIX3LMV43ERDJONRXK43TNFXW4OZWGI4TENRQGQ>
.
You are receiving this because you are subscribed to this thread.Message
ID: ***@***.***>
|
Beta Was this translation helpful? Give feedback.
1 reply
Answer selected by
Gr3q
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Basically, why/how does this work?
The output is:
Beta Was this translation helpful? Give feedback.
All reactions