[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内部.
[MobX] Since strict-mode is enabled, changing (observed) observable values without using an action..
最新推荐文章于 2024-07-18 10:50:15 发布
本文介绍了在使用MobX时遇到的`since strict-mode is enabled`错误,该错误提示在没有使用action的情况下尝试修改observable值。解决方法是引入`runInAction`函数,并将赋值操作包裹在其中,确保异步操作后的赋值在action内进行。这有助于保持状态管理的正确性。
摘要由CSDN通过智能技术生成