小编tws*_*ith的帖子

使用带有异步函数和.then的MobX @action装饰器

我正在使用MobX 2.2.2尝试在异步操作中改变状态.我将MobX的useStrict设置为true.

@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)
  });
}
Run Code Online (Sandbox Code Playgroud)

我明白了:

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
Run Code Online (Sandbox Code Playgroud)

我是否需要将@action装饰器提供给第二个.then语句?任何帮助,将不胜感激.

javascript mobx

14
推荐指数
2
解决办法
4391
查看次数

标签 统计

javascript ×1

mobx ×1