mobx6 and makeAutoObservable produce strict mode warnings #2901
-
If I use makeAutoObservable, I assume no more attention of changing object properties is needed. Simple example: const mobx = require('mobx')
const observableObject = mobx.makeAutoObservable({
a: 1,
b: 2
})
mobx.autorun(() => {
console.log(JSON.stringify(observableObject))
})
setTimeout(() => {
observableObject.a = 2
}, 1500) gives the right output, but also the strict mode warning:
If I replace the setting of the property setTimeout(() => {
mobx.runInAction(() => {
observableObject.a = 2
})
}, 1500)
How can I keep the simplicity of makeAutoObservable for normal Objects without wrapping changes in runInAction? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
The point of the warning is to avoid missing batch, consider: autorun(() => {
console.log(observableObject.a, observableObject.b)
})
setTimeout(() => {
observableObject.a = 2 // first autorun invocation
observableObject.b = 2 // second autorun invocation
}, 1500)
// VS
setTimeout(() => {
runInAction(() => {
observableObject.a = 2
observableObject.b = 2
})
// one autorun invocation at the end of action (outermost if nested)
}, 1500) There is no way for us to distinguish between setting one or multiple props. (Potentially is, but it's not reasonable) |
Beta Was this translation helpful? Give feedback.
The point of the warning is to avoid missing batch, consider:
There is no way for us to distinguish between setting one or multiple props. (Potentially is, but it's not reasonable)
If you don't like this behavior, you can turn it off with
configure({ requireActions: "never" })
You're expected t…