Having played with Vuex and realized how easy it is to mutate states through mutation handler with simple assignment, I am working with redux right now and learned that redux stresses on immutability but it makes coding slightly more verbose. The question now arises,
Surprisingly there is no information I can find on this over internet.
Mutations: In Vuex state isn't immutable because Vuex provides designated functions called mutations for modifying state without entirely replacing it.
Vuex will by default, rightfully throw a warning because the object is mutated oustide of the store mutations. However in some cases it is useful to store such references in the stores as immutable references, in order to persists them and to use them through getters and actions.
Vuex is very similar to Redux and also inspired by Flux. Unlike Redux, Vuex mutates the state rather than making the state immutable. This approach removes the need for having a reducer, so in Vuex reducers are replaced with something called Mutations. This allows Vue.
Mutations are intended to receive input only via their payload and to not produce side effects elsewhere. While actions get a full context to work with, mutations only have the state and the payload .
To answer the questions you need to know how they both work. Veux has a state that is an observable/reactive so setting state.something will trigger anything watching state.something.
Redux state is a "normal" javascript data object (no methods on the object and no prototype should be used). When an action is dispatched then a new state is created by redux and react-redux will run all the mapStateToProps
or useSelector
functions and compare the current result with the last one (using referential compare so {} !== {}
. If there is a change then react-redux will re render that component.
To now answer your questions:
Veux state is not just an object and a lot of listeners are added to state so when you do state.newValue='new value'
it'll trigger all these listeners. Redux needs to compare previous value with current and the most performant way to do it is referential compare. Therefore you have to generate a new state where all unchanged props still point to the same reference as before but changed props point to another reference.
React/Redux uses a functional approach and vue/veux an OO approach. Why it's popular may be because React Redux existed longer and has a better supported ecosystem. Functional programming has become more popular lately because even after decades of trying it's still very hard to write good code in OO (concurrency). The best Uncle Bob can come up with is "A class should have only one reason to change." and written according to that rule you'll end up with a class that has only method which could be done with a function. A problem with combining related data and behavior in a class is that later this relation may change later or you've find that the way you related them does not reflect what needs to be implemented in the real world.
They are both difficult to learn and do well, I find redux way easier to follow and predict what happens in complicated code bases (complex requirements, not needless complexity) and way easier to test but in the end it's just a personal preference.
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