简体   繁体   English

无法分配给 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>'

I have this problem.我有这个问题。

With interface:带接口:

export interface IDevice {
  id: string,
  selected: boolean
}

creating instance by:通过以下方式创建实例:

let newDevice: IDevice = {
  id: uuid4(),
  selected: false,
} as IDevice;

It gets added to array in recoil state, and in React used in a function where array has been retrieved with useRecoilState().它被添加到 recoil state 中的数组中,并在 React 中用于 function 中,其中数组已使用 useRecoilState() 检索。 const [leftList, setLeftList] = React.useState<IDevice[]>([]);

Now it is being used in a handler for selecting the devices on a list control, here the error occurs:现在它在处理程序中用于选择列表控件上的设备,这里发生错误:

...
leftList.map((item: IDevice) => {     
      if (item.id === event.dataItem.id) {
        item.selected = !item.selected;
      }
      return item;
    })
...

And I get the error: Cannot assign to read only property 'selected' of object '#'我收到错误消息:无法分配给 object '#' 的只读属性 'selected'

Even cloning the array first by [...leftList] does not help.即使首先通过 [...leftList] 克隆数组也无济于事。

I'm lost:-) Hope someone can spread light on this?我迷路了:-)希望有人能对此有所了解吗?

State shouldn't be modified directly, which is what you're currently doing now in your .map() method by updating the objects. State 不应该直接修改,这是您现在通过更新对象在.map()方法中所做的。 TS is trying to tell you not to do this by making your objects read-only. TS 试图通过将对象设置为只读来告诉您不要这样做。

Instead, you can create a new object with all the properties from item (done using the spread syntax... ), along with a new overwritting property selected , which will use the negated version of the item's currently selected item if the id matches the event's data item id, or it'll keep the original selected value:相反,您可以创建一个新的 object ,其中包含item的所有属性(使用扩展语法完成... ),以及一个新的覆盖属性selected ,如果 id 与事件的数据项 ID,否则将保留原始选定值:

leftList.map((item: IDevice) => ({
  ...item, 
  selected: item.id === event.dataItem.id ? !item.selected : item.selected
}))

Cloning the array using the spread syntax ( [...leftList] ) will only do a shallow copy, and won't do a deep-copy of the objects within it, as a result, modifying the object references within .map() is still modifying the original state.使用扩展语法( [...leftList] )克隆数组只会进行浅拷贝,不会对其中的对象进行深拷贝,因此会修改.map()中的 object 引用还在修改原来的state。

Or, instead of spreading the item object and creating a new object each time (which can hinder performance a little as pointed out by @3limin4t0r ), you can instead only return a newly created object when you want to modify the selected property:或者,不是每次都传播item object 并创建一个新的 object (这可能会稍微影响性能,正如@3limin4t0r所指出的那样),您可以只返回一个新创建的 ZA8CFDE6331BD59EB2AC96F8911C4B 时修改selected的属性:

leftList.map((item: IDevice) => {     
  if (item.id !== event.dataItem.id) return item;
  return {...item, selected: !item.selected};
});

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

相关问题 无法分配给对象 &#39;# 的只读属性<Object> &#39; - Cannot assign to read only property of object '#<Object>' 无法分配给 # 的只读属性“.js”<Object> - Cannot assign to read only property '.js' of #<Object> 无法分配给 object 的只读属性“displayName” - Cannot assign to read only property 'displayName' of object 无法分配给 object 的只读属性 - cannot assign to read-only property of object Ngrx:无法分配给对象“[Object]”的只读属性“Property” - Ngrx : Cannot assign to read only property 'Property' of object '[Object]' 无法分配给 object '[object Object]' 的只读属性 'name' - Cannot assign to read only property 'name' of object '[object Object]' 未捕获的TypeError:无法分配为只读对象&#39;#的属性&#39;background&#39; <Object> “ - Uncaught TypeError: Cannot assign to read only property 'background' of object '#<Object>' 未捕获的TypeError:无法分配给对象'#<Object>'的只读属性'exports' - Uncaught TypeError: Cannot assign to read only property 'exports' of object '#<Object>' TypeError:无法分配为只读对象“#”的属性“ exports” <Object> 在ReactJS中 - TypeError: Cannot assign to read only property 'exports' of object '#<Object>' in ReactJS 无法分配给对象“#”的只读属性“exports”<Object> &#39; - Cannot assign to read only property 'exports' of object '#<Object>'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM