admin管理员组文章数量:1287868
Hi I'm using MobX in a store and I need to have an async reaction when a puted value has changed:
class Store {
@observable user;
@observable something;
@puted 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 was wondering what would be the difference here with using when
instead of reaction
apart from the running condition ?
when(
() => !!this.firstParam,
async () => {
// fetch using this.firstParam
}
)
Hi I'm using MobX in a store and I need to have an async reaction when a puted value has changed:
class Store {
@observable user;
@observable something;
@puted 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 was wondering what would be the difference here with using when
instead of reaction
apart from the running condition ?
when(
() => !!this.firstParam,
async () => {
// fetch using this.firstParam
}
)
Share
Improve this question
asked Aug 19, 2016 at 10:22
KomoKomo
2,1381 gold badge23 silver badges36 bronze badges
1
- How did you get this working on your machine? – Mike Warren Commented Jul 12, 2019 at 19:18
2 Answers
Reset to default 7Note that when
executes it's effect only once and then stops. So in your case the data would only be fetched once.
reaction(
() => this.firstParam,
async (param) => {
const { data: something } = await axios.get(`url/${param}`);
runInAction('update state after fetching something', () => {
this.something = something;
});
}
);
This will track just this.firstParam
and when that return a new data it will call
async (param) => {
const { data: something } = await axios.get(`url/${param}`);
runInAction('update state after fetching something', () => {
this.something = something;
});
Now, If you go with when
I believe it would end up doing the same,
Taken from mobx docs:
You can use observable data structures as a promise... After pleting the asynchronous action, just update your data
So i see no reason why not to use when
in your case.
本文标签: javascriptMobX async reactionStack Overflow
版权声明:本文标题:javascript - MobX async reaction - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741264275a2368176.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论