您好,我在商店中使用 MobX,我需要在计算值发生变化时进行异步 react :
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 - MobX 异步 react ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39036605/