I have been using Flux first and Redux later for a very long time, and I like them and I see their advantages, but I have one question:
Why do we separate actions and reducers and add additional calls between calls that express an intention to change the state (action) and the actual way of changing the state (reducer), so that it is more difficult to provide static or temporary guarantees and error checking? Why not just use methods or functions that change state?
Methods or functions provide static guarantees (using Typescript or Flow) and runtime guarantees (method / function not found, etc.), while an action that is not processed does not cause errors at all (either static or time execution), you just need to see that the expected behavior is not happening.
Let me demonstrate this a little better with our Theoretical State Container (TSC):
- It's super easy
- Think of it as a React Component state interface (setState, this.state), without any rendering part.
So, the only thing you need is to cause a re-visualization of your components when the state changes in our TSC and the ability to change this state, which in our case will be a simple method that will change this state: fetchData , setError , setLoading , etc.
I see that actions and reducers are the decoupling of dynamic or static code sending, so instead of calling myStateContainer.doSomethingAndUpdateState(...) you call actions.doSomethingAndUpdateState(...) , and you allow the whole thread / reduction mechanism to use this action to the actual state modifications. All of this also introduces the need for thunks, sagas and other middleware to handle more complex actions, instead of using just plain javascript control flows.
The main problem is that for this denouement you need to write a lot of things to achieve this denouement: - the interface of the action creator's functions (arguments) - types of actions - fighting - the form of your state - how you update your state.
Compare this to our theoretical state container (TSC): - the interface of your methods - the form of your state - how you update your state.
So what am I missing here? What are the benefits of this denouement?
This is very similar to this other question: Reducing actions / gears compared to setting state
And let me explain why the most voted answer to this question does not answer either my or the original question: - Actions / Gearboxes allow you to ask questions "Who and how"? this can be done using our TSC, this is just an implementation detail and has nothing to do with actions / gears. - Actions / Reducers allow you to return in time with your state: again, this is a matter of implementation details of the state container and can be achieved using our TSC. - Etc: orders for state changes, middleware and everything that is currently achieved through actions / reducers can be reached using our TSC, it’s just a matter of its implementation.
Thanks a lot! Fran