You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.30 02:05:02