【发布时间】:2016-12-26 11:18:13
【问题描述】:
您好,我在商店中使用 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
}
)
【问题讨论】:
-
你是如何在你的机器上完成这项工作的?
标签: javascript asynchronous store mobx