NGRX状态更新
这是我的状态: -
roles =[ {
roleId:"8e8be141-130d-4e5c-82d2-0a642d4b73e1"
name:"HR"
description:"HR of the Company"
isModerator:"N"
},
{
roleId:"8e8be141-130d-4e5c-82d2-0a642d4b73e1"
name:"MR"
description:"MR of the Company"
isModerator:"Y"
}]
这是我的还原器: -
on(updateRoleItem, (s, { roleId, item }) => {
let modifiedState = [...s];
modifiedState.map((data) => {
if (data.roleId === roleId) {
data.name = item;
}
})
return modifiedState;
})
);
我想更新角色阵列中角色的名称属性。所以我找到了对象 通过Roalid,然后更新名称,但我会遇到错误。有人可以帮助我做正确的方法吗?
this is my State:-
roles =[ {
roleId:"8e8be141-130d-4e5c-82d2-0a642d4b73e1"
name:"HR"
description:"HR of the Company"
isModerator:"N"
},
{
roleId:"8e8be141-130d-4e5c-82d2-0a642d4b73e1"
name:"MR"
description:"MR of the Company"
isModerator:"Y"
}]
this is My reducer:-
on(updateRoleItem, (s, { roleId, item }) => {
let modifiedState = [...s];
modifiedState.map((data) => {
if (data.roleId === roleId) {
data.name = item;
}
})
return modifiedState;
})
);
this is what error I am getting:-
I want to update the name property of role in array of roles. So I am finding the object
by roleId and then updating the name but I am getting error. Can Someone help me with the right way of doing it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
默认情况下,ngrx状态(和动作)为 nofollow noreferrer“>不可能。
这意味着您不能简单地更新属性,但是必须先创建一个克隆。
为此,您可以使用其余的扩展语法。
您也可以使用 ngrx-immer 。
与ngrx-immer
By default the NgRx state (and actions) are immutable.
Which means you can't simply update the properties, but you must create a clone first.
To do this, you can use the rest spread syntax.
You could also use ngrx-immer.
with ngrx-immer