Keeping ngRx Store Immutable

The problem with ngRx store is that it’s not immutable out of the box. Hence, when you use subscribe/select operators to get the state, you’ll always end up getting a reference and not a copy of the state.

Hence, on changing the local copy will actually end up mutating the store.

To fix this, following changes need to be made:

  • Make the store immutable by using immutableJS or ngRx store freeze
  • When you’re selecting/subscribing to the store, make sure that you Don’t change it (coz that’ll break at runtime coz immutableJS will not allow the reference to mutate) OR if you absolutely have to change it, then first make a deep copy of the state before changing the copy
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s