How strict is the requirement for immutability? #290
Comments
Over here we have been a long way down this road. The eventual answer is that you will have endless debugging pain if you allow code that mutates something in the store. Certainly a freeze during development to try to prevent writing code that does this is a good idea. If any application code get something from the store and mutates it, or if reducers sometimes mutate data, obtaining predictable easily understood behavior from the rest of the system downstream from their becomes at least as difficult, possibly more so, than before you started using Store at all. So I don't know about overthinking per se, but the point can hardly be made strongly enough: don't mutate data that's in the store. (By the way, TypeScript 2.1 just shipped, and it brings the object spread operator, which saves considerable Objects.assign() syntax tedium when implementing code in reducers and elsewhere to avoid in-place mutation!) (The more I work with TS the more I'm reminded that sometime in the future it is likely will be writing code in languages that have immutability built in. I've done some work and some of those, and it is much nicer to have the language protecting you from this versus coding standards, libraries, etc.) |
One may also use a Lense library to help with that.. |
How useful would it be for the ngrx/store itself to deep freeze your state in development mode ? On paper, that sound awesome, because you can avoid certain scenarios, like accidentally modifying the store properties, without adding any complexity to your application ( same models, just freeze them ) |
Freezing state in development with something like ngrx-store-freeze is the recommended path to take if you want to guarantee there are no unnecessary state mutations. |
@fxck Ideally "freeze in dev mode" would ship in the box, on by default - rather than being a separate add-on to discover. But it's a tradeoff of course, vs keeping the core as small as possible. |
Hi @kylecordes, what if I have a very deep structure in my store. Something like |
@ericmarcos very good question. I'm running into a similar question, however an even easier use-case: I store a list of things, say, Should I only clone the [ edit ] I now see here that subreducers are the way to go: https://stackoverflow.com/questions/41217604/ngrx-update-single-item-in-a-list-of-items This question even extends to the way Angular's Suppose you have a The only way to combat this as far as I'm researching now, is to use a custom This question applies: I had to do quite a bit of reading and understanding before I understood what was happening... |
I've just started converting one of our apps to use ngrx/store, and I'm hung up a bit on how strict to be with having an immutable store. Does that requirement mean that everything that is in the store must be strictly and deeply immutable? Or just that I shouldn't modify anything that's in the store in a way that would have the side effect of modifying the store? Or somewhere in between?
For example, when looking at one of the reducers in the example app, one of the values in the State is
entities
, which is, essentially, aDictionary<Book>
. I know that none of the selectors returnentities
directly, butgetSelected
returns a Book fromentities
without any sort of defensive copying, even though Book itself isn't immutable.So, couldn't a user grab the selected Book and modify values within the store as a side effect? If so, how is immutability being maintained? I know the example app uses
storeFreeze
to ensure no problems during development, but this seems to be a possible problem.In my code, I have an interface that's something like
interface Foo {index: number; name: string;}
, and I want to put an array or list of them in the store. My initial instinct was to just put myFoo[]
directly in the store, and then I thought that I should have the store actually keepImmutable.List<Foo>
(converting to and fromList
in the reducer and selector). But then I realized thatImmutable.List
is a shallow copy. Should I take further steps to make my store deeply immutable, even though the example app doesn't do that?Am I overthinking this?
Thanks
The text was updated successfully, but these errors were encountered: