Mobx 官方文档指出,您可以对流程返回的 Promise 调用取消。 https://mobx.js.org/best/actions.html只是没有如何执行此操作的示例。
上下文: 在 componentDidMount 中调用异步操作,我们需要在 componentWillUnmount 中取消此操作。还想 setState 表示 ui 可以在 promise 解决后呈现。
componentDidMount() {
this._fetchRawEguide = this.props.combinedEguide.fetchRawEguide(null, true)
.then(() => {
this._fetchRawEguide = null;
this.setState({
loaded: true
});
})
}
componentWillUnmount() {
if (this._fetchRawEguide) {
this._fetchRawEguide.cancel();
}
}
Mobx 操作看起来像这样
@action
fetchRawEguide = flow(function*(date, redirectOnError = false) {
try {
const res = yield request(...);
遇到问题,当它尝试调用它时,它说 .cancel() 不存在。
我尝试将when()与常规await/async一起使用,但似乎不起作用。如果有人有一个等待/异步的例子那就太好了。
最佳答案
这似乎有效
this._fetchRawEguide = this.props.combinedEguide.fetchRawEguide(null, true);
this._fetchRawEguide.then(() => {
this._fetchRawEguide = null;
this.setState({
loaded: true
});
});
我认为将 .then 应用于 flow 返回的 Promise 可能会强制转换它并删除 .cancel 函数
关于reactjs - 我可以获得如何取消 MobX 流程的示例吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57115766/