嗨,我在商店使用MobX,我需要有一個異步反應時計算出的值已經改變:MobX異步反應
class Store {
@observable user;
@observable something;
@computed get firstParam() {
return this.user && this.user.params[0];
}
async loadSomething() {
reaction(
() => this.firstParam,
async (param) => {
const { data: something } = await axios.get(`url/${param}`);
runInAction('update state after fetching something',() => {
this.something = something;
});
}
);
}
}
我想知道什麼是這裏是區別使用when
代替reaction
除了運行條件?
when(
() => !!this.firstParam,
async() => {
// fetch using this.firstParam
}
)