2017-02-02 34 views

回答

3

呦可以這樣做:

//store.js 
import { autorun } from 'mobx'; 
autorun(() => { 
    console.log(store.value); //value is an observable. 
}); 
3

您還可以使用Reaction, 進行日誌記錄,你可能需要使用自動運行,但你應該知道有另一種選擇,這 可以讓您進行更多控制運行你的回調。

import { reaction } from 'mobx' 

class SomeStore { 
    @observable item; 
    @observable otherObservable; 

    constructor() { 
     reaction(
      // The callback will run only on change 
      // of observables described in this function 
      () => this.item, 
      // You can use whatever observables/computed values in this function 
      // without making the function run on an unwanted observables change 
      () => { 
       if (this.otherObservable) { 
        doSometing(); 
       } 
      } 
     ) 
    } 
} 

有更多的選擇此功能,您可以在提供的鏈接讀到它:

因爲語法更有意義我也喜歡它。