Platform: Docs: does state in @ngrx/entity require manual deep copy?

Created on 9 Jan 2020  路  2Comments  路  Source: ngrx/platform

The docs for @ngrx/store point out that the spread operator only makes shallow copies of objects and direct users to look into using deep copy libraries.

My understanding is that they only make sense if state contains nested objects or collections of objects.

From what I've read, deep copies seem to be required only for time travel functionality. Change detection is done via shallow comparison of the state or its slices (ie. root objects), not by comparing each object in the state.

@ngrx/entity however doesn't specify if deep copies would be required if the entities it manages are nested objects (instead of flat ones). I've tried to go through the code on my mobile, and have only seen the spread operator being used, which would indicate deep copies should probably still be made when modifying entities, but I probably missed some finer details.

Can you please confirm if my assumptions are correct, and especially if deep copying (e.g. using immer.js) is advisable when using @ngrx/entity (or if it's taken care of/unnecessary)?

Happy to draft a few sentences for the docs and submit a PR.

Other information:

I would be willing to submit a PR for the docs :heart:

[ x ] Yes (Assistance is provided if you need help submitting a pull request)
[ ] No

Docs Entity

Most helpful comment

I would like to know this too. Does Ngrx provide a copy of the state each time the reducer is called? The docs seem to say it doesn鈥檛, and therefore you should manually deep-copy the state each time.

I can do that, but that leads me to ask: (1) What鈥檚 the best way? JSON.parse(JSON.stringify(state))? and (2) Doing that in every reducer function seems like a real repetitive pain, so if it鈥檚 a requirement why doesn鈥檛 the library just do it?

All 2 comments

I would like to know this too. Does Ngrx provide a copy of the state each time the reducer is called? The docs seem to say it doesn鈥檛, and therefore you should manually deep-copy the state each time.

I can do that, but that leads me to ask: (1) What鈥檚 the best way? JSON.parse(JSON.stringify(state))? and (2) Doing that in every reducer function seems like a real repetitive pain, so if it鈥檚 a requirement why doesn鈥檛 the library just do it?

Practically, I think:

  1. At the time of reducer:
  2. We deal primarily with the state object at its root / first level properties.
  3. Any possible modification will be re-setting a first level property with new value come from action.payload. This is our intention and the new value (even primitive or object) we know the expected value.
  4. For other state's first level properties that we don't tend to change, if the shallow copy of state still refers to their original references, that would be fine. Because:

  5. Redux framework to make sure actions/effects to be handled and pass sequentially to reducer.

  6. At one time, the state should be only examined and modified by one reducer code for a specific action
  7. Also, other places other than reducer (e.g. effects), state object should not be touched directly

Just my thought.
Theoretically regarding the deep copy per NgRx requirement, I'm also curious if it should be strict or not.

Was this page helpful?
0 / 5 - 0 ratings