by badestrand · 8y ago · 0 comments
I think it's the other way around: The greatest weakness of Redux is that (unnecessarily) message passing instead of function calls are used.
Why do a `store.dispatch({type: 'INCREMENT', value: 10})` when it could be a `store.count.increment(10)`? In basically all cases every action is handled by exactly one reducer so just let the reducer be an object with methods instead of a big switch function operating on string constants.
IMHO you want the tools you use (i.e. redux) to be flexible and powerful, and write helpers, etc, to increase your ease-of-use. Otherwise, as the OP said, you're unnecessarily restricting yourself and creating a more difficult future.
Thanks for your input! Do you mind giving 2-3 examples of good uses of multiple reducers for the same action?
Another example is url updates, I use state-driven-routing and when a url update happens many reducers need to respond to get the pieces of state they are concerned about.
Another example is a login status change, many reducers may need to update themselves if a user logs in our logs out.
It's the difference between having tons of highly specific 1:1 actions getting called from the action creator with their relationship to the root action obfuscated vs. having 1 action that many respond to with their relationship to that action clearly denoted.
To take it a bit further, it reminds me of refactoring vs adding another "if" statement - the former requires comprehensive understanding of the holistic purpose of the system with the dependent relationships properly defined (anti-fragile), whereas the latter is a temporary bandaid that can be implemented with less thought (and the sideeffects that come with it).
Wouldn't the components handle this change and dispatch any actions needed?
> a login status change, many reducers may need to update themselves if a user logs in our logs out.
Again, couldn't this be handled at the component level? It seems risky to have multiple sources of truth for data.
Because this is how the rest of your programming works, you call functions. In Node you don't have `fs.processMessage('readFile', {path, next})` because `fs.readFile(path, next)` is simpler. Message passing is nice if you pass process boundaries or can't know about the function definition but in Redux you pull in the string constants and everything anyway. Message passing also lets you hook several handlers (reducers) to the same message but I have yet to see it significantly used, this is a niche case.
It lets static typing do a lot more work for you when using Typescript/Flow. Less to remember and less to break.
Message passing is a really useful architectural choice because it lets you do powerful global operations. It's a lot crummier on the development ergonomics front.