timdeschryver / ngrx-immer

Immer wrappers around NgRx methods createReducer, on, and ComponentStore
https://www.npmjs.com/package/ngrx-immer
MIT License
120 stars 7 forks source link

Cannot assign to read only property #22

Closed DiegoSestito closed 10 months ago

DiegoSestito commented 10 months ago

Here is my state and reducer: `export interface AppState { hexMatrix: HexMatrixState; }

export interface HexMatrixState { hexLines: HexLine[]; }

export const initialState: HexMatrixState = { hexLines: [], };

export const hexMatrixReducer = createReducer( initialState, immerOn(loadHexMatrix, (state, action) => { state.hexLines = [...action.hexMatrix.hexLines]; return state; }), immerOn(updateHex, (state, action) => { state.hexLines[action.hexCoordenates.line].hexes[ action.hexCoordenates.column ].hexType = action.hexType; return state; }) );`

When I try to execute "updateHex" action I got this error: "ERROR TypeError: Cannot assign to read only property 'hexType' of object '[object Object]'"

Here is the complete stacktrace: hex-matrix-facade.ts:27 ERROR TypeError: Cannot assign to read only property 'hexType' of object '[object Object]' at hex-matrix.reducer.ts:30:7 at ngrx-immer-shared.mjs:9:42 at Immer2.produce (immer.mjs:570:20) at ngrx-immer-shared.mjs:9:16 at ngrx-store.mjs:1682:22 at combination (ngrx-store.mjs:345:31) at ngrx-store.mjs:1108:23 at ngrx-store.mjs:375:14 at computeNextEntry (ngrx-store-devtools.mjs:506:17) at recomputeStates (ngrx-store-devtools.mjs:536:48)

timdeschryver commented 10 months ago

Can you create a runnable reproduction please @DiegoSestito ?

DiegoSestito commented 10 months ago

Here it is: https://stackblitz.com/edit/stackblitz-starters-tbq1ri?file=src%2Fstate%2Fhex-matrix.reducer.ts

timdeschryver commented 10 months ago

@DiegoSestito see https://immerjs.github.io/immer/complex-objects/

Fixed reproduction: https://stackblitz.com/edit/stackblitz-starters-6wzdmg?file=src%2Fstate%2Fhex-matrix.reducer.ts,src%2Fmain.ts