Using immer can not modify property Number of nested object

I am using Angular 2+ with Redux and is fine,
but can not modify quantity with the new value

The error is:
TypeError: Cannot assign to read only property ‘quantity’ of object ‘[object Object]’

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
  })
}

1 thought on “Using immer can not modify property Number of nested object”

  1. because in redux you can’t modify the value of the state
    u have to copy the values
    so use map
    instead of find
    and i hope things gose well

    Reply

Leave a Comment