有沒有什麼辦法可以在任何一個mobx @observable
改變值時自動觸發console.log
?`console.log`一個mobx`@ observable`只要其值改變
我會用mobx開發工具來完成它,但它會觸發大量的控制檯日誌,所以很難指出它正在跟蹤的值。
有沒有什麼辦法可以在任何一個mobx @observable
改變值時自動觸發console.log
?`console.log`一個mobx`@ observable`只要其值改變
我會用mobx開發工具來完成它,但它會觸發大量的控制檯日誌,所以很難指出它正在跟蹤的值。
呦可以這樣做:
//store.js
import { autorun } from 'mobx';
autorun(() => {
console.log(store.value); //value is an observable.
});
您還可以使用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();
}
}
)
}
}
有更多的選擇此功能,您可以在提供的鏈接讀到它:
因爲語法更有意義我也喜歡它。