如何解决使用 immer 不能修改属性嵌套对象的数量
我在 Redux 中使用 Angular 2+ 并且很好, 但不能用新值修改数量
错误是: 类型错误:无法分配给对象“[object Object]”的只读属性“quantity”
export interface CartState {
readonly cars: CartCarModel[]
}
export class CartCarModel {
constructor (
public carId: string,public carModel: string,public quantity: number,public price: number) { }
}
const initialState: CartState = {
cars: []
}
function reducerFunc (state: CartState,id: string,quantity: number) {
const nextState = produce(state,draft => {
draft.cars.find(d => d.carId === id).quantity = quantity
})
return Object.assign({},state,{
drinks: nextState
})
}
解决方法
因为在 redux 中你不能修改 state 的值
你必须复制值
所以使用map
而不是find
我希望一切顺利
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。