DEV Community

Discussion on: Redux is half of a pattern (1/2)

Collapse
 
jontymc profile image
Jonathan Curtis

Interesting post and made me clarify my thoughts about Redux. I would argue that actually Redux is trying to be more than it should and this is a big source of confusion to people trying to learn it.

For example, whether you use a state machine or not should have nothing to do with the state itself. Why do you need a reducer? Just have your own state machine logic and update the state using events from the state machine.

Redux should just really be about updating state with events, reducers muddy the waters. I think this muddiness comes from its origins of use with react and specifically the idea of uni-directional data flow. Practically, state management should be agnostic of uni-directional data flow, but this is entrenched in Redux design.

Middleware and async actions also lead people down the wrong path. Mostly this is just code you should have in plain code you write. Eg, loading data should be outside of state management. Load data, update state. Don't complicated with middleware.

I prefer libraries that do one thing well. A state management system should just be about events, state and subscribing to changes. Redux has confusing concepts like reducers, async actions and middleware, not to mention actions are events. It would be much better split into two parts - state management and state machine, although arguable whether you need a library for the second part.

Collapse
 
jontymc profile image
Jonathan Curtis • Edited

I would also argue that the loading state of an api request does not belong in the global state. This is local to a single component, putting local un-shared state in the global state makes things more complicated.

Collapse
 
davidkpiano profile image
David K. 🎹

Completely agree. I'll cover this more in the second article. Nothing belongs to global state because nothing truly is global state.

Thread Thread
 
boubiyeah profile image
Alex Galays

That's my main gripe with redux.

I think no more than 20-30% of state should be global: logged user and its organization, resource cache and that's pretty much it.
It's insane to have to go through all the selectors for some fairly local piece of state changed.

Thread Thread
 
luiz0x29a profile image
Real AI

Partial update of state in immutable pure functional applications is pretty much open research.
We actually need machinery to deal with it, you can't just update the global state every time.
You just happen to now have the same problem again, instead of React and DOM, you reintroduced it with your entire application global state in place of the DOM, and now need some form of React to deal with it.

Perhaps the entire perspective was wrong.
Virtual Finite State machines is what I'm looking into, they contain the mutable state, the are pure in the way that state+event change state, and they allow for side effects, because without side-effects:

"Haskell is a completely useless language because in the end a program with no effects, there's no point in running it, is a useless program.
And you know, you have this black box, and you press go, and it gets hot, but there's no output.
So why did you run the program" - Simon Peyton Jones

youtube.com/watch?v=iSmkqocn0oQ