我可以用Mobx商店制作可观察变量吗



初始化存储时,我有一个变量,用于创建新的子存储。重新编辑此子存储时,观察器组件不会重新提交更改。

示例

class OrderStore {
price: PriceStore; // not observable

@action.bound
async fetch(){
const response = await fetchOrder();
this.price = new PriceStore(response.price);
}

@action.bound
registerRefreshOrder(){
setInterval(()=>{
this.fetch();
}, 10000);
}
}

class PriceStore{
@observable amount:number;
constructor(priceResponse:{amount:number}){
this.amount = priceResponse.amount;
}
}

因此,当price变量不可观察时,观察者组件没有更新PriceStore.amount更改。

对不对?我应该初始化存储一次还是可以重新初始化?当我创建存储的可观察实例时,它使该对象的所有字段或唯一链接都可观察?

看起来我找到了解决方案。对于可观察存储实例,需要使用@observable.ref

最新更新