What is the necessity of actions in Alt.js

214 Views Asked by At

Iam new to react. I have been reading Alt.js and flux architecture since few days. Few conceps I am not able to understand. From a component we access store and get the data. If it is server side request we have async Data Sources which is binded to the store through this.registerAsync(). Any POST or GET request made to the server are all written in Data Source file and the response is handled in the store. So what is the necessity of actions I see actions file as just a dummy file which useless connects source and store or component and store. Rather can't we remove action and directly send response to store from source or from the component ?

1

There are 1 best solutions below

0
HosseinAgha On

We personally used NuclearJS (another flux implementation similar to one you use) and had the same question for a long time.
It is not a necessary thing. Redux does not have actions for example.
After building a large application using NuclearJS we have a internal rule for how to use actions and we found them rather useful. Imagine you have a form that on its change you are going to get new records and update the records list in store, update form values and also change a isLoading state till we completely updated the list by new records. Here you can create an action that dispatches 3 times for each of the updates above.
Also for actions that were only dispatching their corresponding store, we added a wrapper api that whenever an action was a 'delegateToStore' string (not a function) that api calls the store directly, and we just write 'delegateToStore' in our actions.
Finally you don't even need a store unless your app is complicated enough, I suggest that you start with a simpler flux like library like redux and gradually move to complicated solutions (like relay, etc) as your application becomes much more (Facebook scale!) complicated.