繁体   English   中英

无法分配给 object 的只读属性

[英]cannot assign to read-only property of object

#interestingProblem 任何人都可以解释一下,我在更新 state 时遇到了问题,就像在第一个代码块中一样,但是当我在第二个代码块中更新 state 时没有问题,如下所示。

我有一个问题:(不能分配给 object 数量的只读属性)

const newItem = action.payload
newItem.quantity = 1
state.items = [...state.items, newItem]

当我写这样的代码时我没有问题

const newItem = action.payload
state.items = [...state.items, { ...newItem, quantity: 1 }]

第一种方法是直接改变action.payload ,因为您没有创建到newItem的副本,而是传递相同的引用。 鉴于action.payload是只读的,您将面临错误:

// passing the same reference, 'newItem' points to 'action.payload'
// hence newItem is not copy
const newItem = action.payload
// here you mutate 'action.payload' since 'newItem' points to same reference
newItem.quantity = 1
state.items = [...state.items, newItem]

第二种方法有效,因为您正在从action.payload创建副本而不是对其进行变异:

// here 'newItem' still points to same reference 'action.payload'
const newItem = action.payload
// but here you are spreading the values into a new object, not mutating directly
state.items = [...state.items, { ...newItem, quantity: 1 }]

相反,您应该首先为您的工作方法创建一个副本:

// here you create a new object from 'action.payload''action.payload'
// hence newItem contains the same values but it's a different object
const newItem = { ...action.payload }
// now you are not mutating 'action.payload', only 'newItem' that's a new object
newItem.quantity = 1
state.items = [...state.items, newItem]

action.payload可能是只读的 object。 在第二个代码块上,扩展运算符将键值对传递给新的 object。

因为当我假设在 React 中使用 state 执行类似的 **kwargs 时,您将没有嵌套的 state 传递给具有嵌套 state 的目标,因为它声明了重新评估它的目标。

无法分配给 object '# 的只读属性'property'<object> '<div id="text_translate"><p> 我有这个问题。</p><p> 带接口:</p><pre> export interface IDevice { id: string, selected: boolean }</pre><p> 通过以下方式创建实例:</p><pre> let newDevice: IDevice = { id: uuid4(), selected: false, } as IDevice;</pre><p> 它被添加到 recoil state 中的数组中,并在 React 中用于 function 中,其中数组已使用 useRecoilState() 检索。 const [leftList, setLeftList] = React.useState&lt;IDevice[]&gt;([]);</p><p> 现在它在处理程序中用于选择列表控件上的设备,这里发生错误:</p><pre> ... leftList.map((item: IDevice) =&gt; { if (item.id === event.dataItem.id) { item.selected =.item;selected; } return item. })...</pre><p> 我收到错误消息:无法分配给 object '#' 的只读属性 'selected'</p><p> 即使首先通过 [...leftList] 克隆数组也无济于事。</p><p> 我迷路了:-)希望有人能对此有所了解吗?</p></div></object>

[英]Cannot assign to read only property 'property' of object '#<Object>'

无法分配给 object 的只读属性(值)'#<object> '<div id="text_translate"><p> 我有一个 function 检索 object。</p><pre> const removeLogo = (values: any) =&gt; { values.settings.logoUrl = undefined; setMaster({...values }); };</pre><p> 我的 object 如下:</p><pre> values = { name: "test", reference: "ref" settings: { logoUrl: 'url' } }</pre><p> 在这个 object 我只是想改变一个值:</p><pre> values.settings.logoUrl = undefined;</pre><p> 但我最终收到以下错误消息:</p><pre> form-mail-reminders.tsx:83 Uncaught TypeError: Cannot assign to read only property 'logoUrl' of object '#&lt;Object&gt;'</pre><p> 有没有办法修改object的值而不出现这个错误信息?</p></div></object>

[英]Cannot assign to read only property (value) of object '#<Object>'

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 角度2 | 无法分配给数组,因为它是常量或只读属性 无法分配给对象 &#39;# 的只读属性<Object> &#39; 无法分配给 # 的只读属性“.js”<Object> 无法分配给 object 的只读属性“displayName” 即使属性未标记为只读,打字稿“无法分配给属性,因为它是常量或只读” Ngrx:无法分配给对象“[Object]”的只读属性“Property” 无法分配给 object '# 的只读属性'property'<object> '<div id="text_translate"><p> 我有这个问题。</p><p> 带接口:</p><pre> export interface IDevice { id: string, selected: boolean }</pre><p> 通过以下方式创建实例:</p><pre> let newDevice: IDevice = { id: uuid4(), selected: false, } as IDevice;</pre><p> 它被添加到 recoil state 中的数组中,并在 React 中用于 function 中,其中数组已使用 useRecoilState() 检索。 const [leftList, setLeftList] = React.useState&lt;IDevice[]&gt;([]);</p><p> 现在它在处理程序中用于选择列表控件上的设备,这里发生错误:</p><pre> ... leftList.map((item: IDevice) =&gt; { if (item.id === event.dataItem.id) { item.selected =.item;selected; } return item. })...</pre><p> 我收到错误消息:无法分配给 object '#' 的只读属性 'selected'</p><p> 即使首先通过 [...leftList] 克隆数组也无济于事。</p><p> 我迷路了:-)希望有人能对此有所了解吗?</p></div></object> 无法分配给 object '[object Object]' 的只读属性 'name' 获取错误:&#39;无法分配到&#39;location&#39;,因为它是一个常量或只读属性&#39;与Angular app中的mailto函数 无法分配给 object 的只读属性(值)'#<object> '<div id="text_translate"><p> 我有一个 function 检索 object。</p><pre> const removeLogo = (values: any) =&gt; { values.settings.logoUrl = undefined; setMaster({...values }); };</pre><p> 我的 object 如下:</p><pre> values = { name: "test", reference: "ref" settings: { logoUrl: 'url' } }</pre><p> 在这个 object 我只是想改变一个值:</p><pre> values.settings.logoUrl = undefined;</pre><p> 但我最终收到以下错误消息:</p><pre> form-mail-reminders.tsx:83 Uncaught TypeError: Cannot assign to read only property 'logoUrl' of object '#&lt;Object&gt;'</pre><p> 有没有办法修改object的值而不出现这个错误信息?</p></div></object>
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM