createViewModel脏状态异常:恢复原值后仍显示为脏
Mobx createViewModel 脏状态判定异常问题
使用createViewModel处理状态时遇到异常:
- 将
address属性修改为其他值时,isDirty能正确判定为true,符合预期 - 但将
address恢复为初始旧值后,isDirty仍然显示为true,不符合预期
已显式将address属性设置为observable.struct,确保对比的是属性值而非引用,当前使用版本为mobx@6.10.0和mobx-utils@6.0.8。
复现代码
const { autorun, action, observable } = require("mobx"); const { createViewModel } = require("mobx-utils"); function Todo() { const person = observable({ name: 'tarun', address: { firstLine: 'this is the main address', secondLine: 'second thing' } }, { address: observable.struct }); const personViewModel = createViewModel(person); autorun(() => { console.log('personViewModel?.address.firstLine', personViewModel?.address.firstLine); console.log('personViewModel?.address.secondLine', personViewModel?.address.secondLine) console.log('personViewModel.isDirty', personViewModel?.isDirty) }) const update = action((address) => { personViewModel.address = address; }) return { update } } const { update } = Todo(); // isDirty变为true,符合预期 update({ firstLine: 'edit first 1', secondLine: 'edit second 1' }); // 重置address后,isDirty仍显示为true,不符合预期 update({ firstLine: 'this is the main address', secondLine: 'second thing' })
内容的提问来源于stack exchange,提问作者Tarun Singh
相关产品推荐
相关产品推荐

