Updating an object in the ngrx/store

前端 未结 4 1663
無奈伤痛
無奈伤痛 2021-01-04 21:52

I\'m using @ngrx/store for an Angular 2 app.

My store holds a list of say, Book objects. I want to update a field in one of those objects. I also happ

4条回答
  •  轮回少年
    2021-01-04 22:40

    I'll have to make an assumption about the actual scenario the OP is experiencing.

    The problem

    It's not possible to modify a member of a frozen object. Its the error being thrown.

    The cause

    ngrx-store-freeze is used as a meta-reducer to freeze any object that enters the store. On another place, when an object needs to be changed, a shallow copy is being made. Object.assign() doesn't do deep copy. A member of another object reached from the original object is being modified. This secondary object is also frozen, by it is not duplicated.

    Solution

    Use a deep copy like cloneDeep() from lodash. Or sent a bag of properties to be changed with a proper action. Process the changes on the reducer.

提交回复
热议问题