Hey there, I am currently utilizing MobX in a store and faced with the need for an asynchronous reaction to occur when a computed value changes:
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;
});
}
);
}
}
I'm intrigued about the potential differences between using when
instead of reaction
, aside from just the running condition. Any thoughts?
when(
() => !!this.firstParam,
async () => {
// fetch using this.firstParam
}
)