嗨,我在商店中使用 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
}
)