3 ms·
Yes, Redux is easy. The problem is the incredible amount of boilerplate and repeated code in most tutorials and beginner projects, in the form of actions, switc
by ratww 7y ago
Yes, Redux is easy. The problem is the incredible amount of boilerplate and repeated code in most tutorials and beginner projects, in the form of actions, switch statements and ALL_CAPS_STUFF.
That boilerplate can easily take 10x more lines than "real" code in reducers, even in complex applications. Requiring three different files for each reducer, as in the article presented, is completely overkill, and it only makes sense when you're getting paid by the line of code.
However, there are great solutions to avoid that. I'll echo the recommendation of createSlice:
https://redux-starter-kit.js.org/api/createslice https://redux-starter-kit.js.org/api/createslice
- Izkata 7y ago> Requiring three different files for each reducer, as in the article presented, is completely overkill He only has one [0]. Note that the parent directory is "redux", not "reducer" - the actions and actionTypes are separate from the reducers, because there isn't a 1:1 relationship. This separation is because you could, for example, have a second reducer that listens to the same Add action as the one in this example - it doesn't need it's own action or actionType. [0] https://miro.medium.com/max/1383/1*JsqDyA2Wy_08GnHS6SMDmw.png https://miro.medium.com/max/1383/1*JsqDyA2Wy_08GnHS6SMDmw.pn...
- ratww 7y agoWhile you're technically correct, this is exactly what's overkill. Actions and ActionTypes are pure noise. And idk, having multiple reducers responding to the same action is a recipe for trouble.
- acemarke 7y agoI'll disagree with you on that last point. We specifically encourage that pattern - treating actions as "events" that any part of the reducer logic can respond to independently, rather than "setters". Now, it's certainly true that _most_ actions will only be handled by a single slice reducer, but having many slice reducers responding to the same action is an incredibly useful pattern: https://redux.js.org/faq/actions#is-there-always-a-one-to-one-mapping-between-reducers-and-actions https://redux.js.org/faq/actions#is-there-always-a-one-to-on...
- ratww 7y agoI do get the point of it being this way, but my problem with it is that it feels too much like magic, I think I just prefer to have the 1:1 relationship, like I already do with javascript functions or HTTP calls. But even leaving that aside, I still think that this specific "feature" is not worth the amount of boilerplate you need in most Redux apps, so it's not a nice tradeoff IMO :( I worked in some bigger apps, and before adopting ducks (and later a homebrew version of createSlice) we had some bugs caused by multiple reducers firing. I also think that it makes Flux/Redux a bit harder to explain
- Tade0 7y agoYou're not alone in this. Redux isn't complex, it's complicated - needlessly so. It takes just one look at its competitors to see this.
- krzepah 7y agoCheckout Unistore, you might like it, it involves less boilerplate code