Mobx 反应不跟踪从构造函数参数传递的可观察值
Mobx reaction not tracking observable value passed from constructor parameter
我有这个class:
class Mother{
@observable
name: string
child: Child = new Child(this)
constructor(){
makeObservable(this)
}
}
class Child{
constructor(mother: Mother){
const disposer = reaction(() => [mother.name], ()=>{})
console.log(getDependencyTree(disposer));
}
}
输出显示处理程序对 mother.name 没有依赖性。我错过了什么?
问题是初始化的顺序:子对象在调用 makeObservable(this) 之前初始化,因此 mother.name 在 运行 反应时还不是可观察的。通过在调用 makeObservable(this) 之后移动子对象的初始化,我解决了这个问题。
我有这个class:
class Mother{
@observable
name: string
child: Child = new Child(this)
constructor(){
makeObservable(this)
}
}
class Child{
constructor(mother: Mother){
const disposer = reaction(() => [mother.name], ()=>{})
console.log(getDependencyTree(disposer));
}
}
输出显示处理程序对 mother.name 没有依赖性。我错过了什么?
问题是初始化的顺序:子对象在调用 makeObservable(this) 之前初始化,因此 mother.name 在 运行 反应时还不是可观察的。通过在调用 makeObservable(this) 之后移动子对象的初始化,我解决了这个问题。