[MobX] Since strict-mode is enabled, changing (observed) observable values without using an action..

本文介绍了在使用MobX时遇到的`since strict-mode is enabled`错误,该错误提示在没有使用action的情况下尝试修改observable值。解决方法是引入`runInAction`函数,并将赋值操作包裹在其中,确保异步操作后的赋值在action内进行。这有助于保持状态管理的正确性。
摘要由CSDN通过智能技术生成

[MobX] Since strict-mode is enabled, changing (observed) observable values without using an action is not allowed. Tried to modify:
报错源代码展示:
async test() {
const testResult = await test()
this.testNumber = testResult
}
处理方式:
1.引入runInAction
import { runInAction } from ‘mobx’
2.在runInAction 中赋值
async test() {
const testResult = await test()
runInAction(() => {
this.testNumber = testResult
})
}
导致的原因
mobx中只能在acrion中重新赋值,异步导致赋值操作被加载到队列中,在action外面了,
runInAction 函数将赋值操作包裹在action内部.

  • 6
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值