我正在使用MobX 2.2.2來嘗試改變異步操作中的狀態。我將MobX的useStrict設置爲true。使用帶異步函數的MobX @action修飾器和.then
@action someAsyncFunction(args) {
fetch(`http://localhost:8080/some_url`, {
method: 'POST',
body: {
args
}
})
.then(res => res.json())
.then(json => this.someStateProperty = json)
.catch(error => {
throw new Error(error)
});
}
我得到:
Error: Error: [mobx] Invariant failed: It is not allowed to create or change state outside an `action` when MobX is in strict mode. Wrap the current method in `action` if this state change is intended
我需要的@action裝飾提供給第二。那麼語句?任何幫助,將不勝感激。
如何做到這一點在異步/的await? 「 – TangMonk