Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/27.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 我可以用Mobx store生成可观察变量吗?_Javascript_Reactjs_Mobx_Mobx React - Fatal编程技术网

Javascript 我可以用Mobx store生成可观察变量吗?

Javascript 我可以用Mobx store生成可观察变量吗?,javascript,reactjs,mobx,mobx-react,Javascript,Reactjs,Mobx,Mobx React,初始化存储时,我有一个变量,其中创建了一个新的子存储。重新初始化此子存储时,observer组件不会重新引发更改 范例 class OrderStore { price: PriceStore; // not observable @action.bound async fetch(){ const response = await fetchOrder(); this.price = new PriceStore(response.price)

初始化存储时,我有一个变量,其中创建了一个新的子存储。重新初始化此子存储时,observer组件不会重新引发更改

范例

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;
   }
}
因此,虽然价格变量不可见,但观察者组件并没有更新PriceStore.amount更改


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

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