My states are of type Immutable.js in my redux store and I only change the state in my reducers. Would it not make more sense to skip using immutable and have a parser that checks my app on command and warns if I change my store outside the reducer functions? Or is there a good reason as to why this isn't done?
You can mutate your state in your reducers if you feel like it. Redux does not mandate that you don't mutate your state but it advises against it.
React-Redux, which isn't Redux, is designed with the assumption that state never gets mutated. This allows it to do very fast checks to find out if something changed anywhere in the state hierarchy.
Redux itself will fire an event each time an action is processed, whether it modifies (by mutating or by replacing) the state or not. It never checks state except for undefined
. It doesn't care what you do with it.
Immutable state has many benefits but the main one used in Redux is that it's fast to check for modifications. Your "parser", for instance, would need to visit every property and item of every object and array in your entire state each time it runs for it to make a meaningful determination. If React-Redux did the same thing then it would be horribly slow and no one would use it.
So, thousands of developers are constraining themselves to immutablity because it has real advantages beyond academics.
I think your idea of some type of middleware that could check for inadvertent state mutations is a really cool idea, actually. I'm not sure how feasible/performant it would be (it may even exist already and I just haven't heard of it). But, for those not using some type of library such as Immutable.js or seamless-immutable, I can see how that would be useful.
I just want to clarify that Immutable.js is enforcing that you are not mutating your state, and it's just a consequence of this is that it becomes impossible to change your state from outside of your reducers. There is an important difference between the general programming concept of immutability and the Redux-specific concept of only using reducers to change your state. Immutable.js enforces the former and thereby logically also enforces the latter.
However, the fact that Immutable.js ensures you can't mutate state accidentally is not the only reason for using the library for your Redux store:
return state
.updateIn(['todos', '5', 'attachment'], attachment => attachment.set('name', 'foo'))
.set('isProcessing', true)
.updateIn(['other', 'nested', 'stuff'], stuff => stuff.set('bar', 'baz'));
That being said, it's definitely optional. You've got alternatives like the aforementioned seamless-immutable, lodash-fp, and just doing it manually like so:
return Object.assign({}, state, { name: 'new name' });
For comparison, the Immutable.js alternative would just be:
return state.set('name', 'new name');
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With