Ole
Ole

Reputation: 46920

Do we get object immutability automatically when using ngrx or ngxs?

Do we automatically get object immutability for all domain instances when using NGRX or NGXS? Most of the contexts that I read up on seem to indicate this, but there's also wording like"When we use NGRX with immutable objects ...", so just want to double check that we are not suppose to use something like immutable.js in addition to ngrx?

Upvotes: 4

Views: 867

Answers (2)

Lucas
Lucas

Reputation: 10303

NGXS does use immutable objects when working on development mode. Please refer to state-operations.ts file in ngxs/store repo to verify this.

Start in line 36 and follow the code:

if (this._config.developmentMode) {
  return this.ensureStateAndActionsAreImmutable(rootStateOperations);
}

In ensureStateAndActionsAreImmutable there is deepFreeze() which does an Object.freeze(value) on values.

So no need for immutable, immer or any immutable library when using ngxs.

If you go with NGRX, you might want to use ngrx-store-freeze instead of third party libs. This has one dependency(deep-freeze-strict) that will ensure immutability.

Upvotes: 2

Zygimantas
Zygimantas

Reputation: 8767

No, we don't. If you don't use immutable library, passed payloads must be deeply cloned manually inside reducer (NGRX) or action handler (NGXS) before added to the state.

Upvotes: 4

Related Questions