0

例如,

state = {
  data: {}
}

如何将新的嵌套字段添加到对象中?我无法设置该字段,因为有错误Cannot read property 'date' of undefined

const reducer = produce((draft, action) => {
   switch (action.type) {
      case 'ACTION_SUCCESS':
      draft.data.children.date = action.response;
   }
});

结果我想要:

  data: {
     children: {
        data: 'date'
     }
  }
}
4

1 回答 1

1

普通的 JS 对象操作规则适用于此。obj.x.y.z =如果还没有字段,你就不能写.y——你必须先创建它。

于 2021-04-15T15:20:11.110 回答