javascript - 为什么不能直接更新对象中嵌套属性的值?
问题描述
我在 React Native 中玩耍时遇到了这个错误。我不明白为什么第一个片段会引发错误,而第二个片段运行良好。
这不起作用:
let newContent = { ...oldContent }; // <-- oldContent is a 'const'
newContent.value = newValue; // <-- property at 'root level' can be modified
newContent.nested.a = b; // <-- throws 'You attempted to set the key with the value on an object that is meant to be immutable and has been frozen.'
但这很好用:
let newContent = { ...oldContent };
newContent.value = newValue;
newContent.nested = {
a: b,
}
的值nested.a
定义在 上oldContent
。
这是预期的行为还是我在第一个片段中做错了什么?
解决方案
推荐阅读
- python - 如何检查 QThread 中是否存在文件
- kubernetes - Kubernetes 获取完整的 pod 名称作为环境变量
- r - R:合并重复条目并按组有条件地选择日期
- c++ - 不能在 Switch Case 中调用函数 - C++
- php - 未定义的方法 Laravel\Lumen\Application::booted()
- angular - 无法在 Leaflet-geotiff 上绘制 geotiff
- mysql - only_full_group_by 不让我显示我想要的表格
- composer-php - 作曲家:如何释放作曲家(演出)使用的大量磁盘空间?
- windows - OpenDDS - 解决方案构建良好,示例运行,但 IDL exe 崩溃
- angular - 角度材料日期选择器在焦点离开时自动选择日期