4 ms·
> You can make API calls in your actions just like you normally would Jesus don't do this. Mobx, Vuex, Redux, etc, are about managing application state not abo
by pier25 7y ago
> You can make API calls in your actions just like you normally would
Jesus don't do this. Mobx, Vuex, Redux, etc, are about managing application state not about managing application logic.
I started making SPAs in 2015 and I also used actions for everything (API calls, auth, etc). It was fine for small projects but then I got to work on a medium sized project and those actions became huge and controlled everything in my app. I usually consider myself a pragmatic person but that was impractical for a number of reasons and also conceptually wrong.
When working in SPAs now I remove anything that is not state management from MobX/Vuex/etc (I don't use Redux anymore). That logic belongs somewhere else.
The pattern I'm doing now is that I conceptually separate components from the application logic. The store(s) feed data to the components but if the components want to interact with the application they go through a mediator of sorts (eg: changing a route, modifying state, authenticating, etc). This mediator then interacts and coordinates with the different application modules.
For example when authenticating many things have to happen in different modules (API, store, router):
1) An API call has to be made to verify credentials and receive a token
2) This token has be stored somewhere eg: localStorage
3) Maybe we also need to make an API call to get user info, their roles, etc
4) Some state in the store has to be modified. Not only the user info, but the app logic usually needs to know if a user is authenticated.
5) We need to tell the router to go to somewhere eg: /home
It makes no sense to do all that in an action. Store actions should be about managing state, period.
- mpfundstein 7y agoWhat about redux-thunk? I usually put all me data fetching logic in thunjs and dispatch to regular actions if needed.
- pier25 7y agoI haven't used it but AFAIK redux-thunk allows you add async middleware to redux. I don't see how adding more features to your store would alleviate the architectural problems I've mentioned. If anything it makes you store even a bigger protagonist in your application.
- nawitus 7y agoWhat does "do in action" mean? If you mean "make API call in reducer", that is commonly known to be an anti-pattern. Triggering the API call by dispatching action, however, is a common pattern. Usually it's implemented with a library like redux-saga or redux-observable.
- pier25 7y agoI agree it's better if the implementation of, say, the API is not done in actions. Although I do not agree actions should be responsible for triggering application logic. It is a very popular pattern indeed, and I've used myself a number of times, but my code makes a lot more sense now that store(s) are only responsible for state management.
- hashamali 7y agoI think this is generally a side effect of people considering actions to be a replacement for the Controller part of MVC. Meaning any sort of logic (including legitimate state manipulation) is jammed into actions. Personally, I don't see that as a huge problem. Better in an action than sprinkled into components. As someone else mentioned, redux-thunk makes this even easier (and one could argue, promotes this).
- nohuhu 7y agoAnother way of doing controller stuff is by actually using a Controller: https://github.com/riptano/statium#viewcontroller https://github.com/riptano/statium#viewcontroller. Example: https://github.com/nohuhu/react-statium-realworld-example-app/blob/master/src/components/Register.js#L117 https://github.com/nohuhu/react-statium-realworld-example-ap... Logic flow doesn't have to be hard to do.
- pier25 7y agoYes, I agree on the MVC part. > Better in an action than sprinkled into components Totally.