Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

React + Redux App: Calling API through a Redux action Vs Calling an API directly

I'm working on a React+Redux application. I'm calling APIs through Redux actions and storing the response data in the Redux state. But there is a case, Where I don't have to store the API response data in the Redux store.

So the question is, Is there any valid reason to call the APIs through Redux actions or Should I call the APIs directly since I'm not storing the response data in Redux store?

like image 878
Abhilash Reddy Avatar asked Mar 07 '23 11:03

Abhilash Reddy


1 Answers

It depends on what kind of call you're trying to make, and who's concern it is.
Here are a few cases:

  • Is this a one-way call to track something?. You can fire an action that gets picked up in a middleware. this is a good case for sending analytics. This doesn't have to be stored in Redux's store.

  • Is this a call where some other part of your application will need this data?, then this is a good use case for making an update in the Redux Store so other components when read this and use props to decide what to render etc.

  • Is this a call where it only concerns one component or isolated part?. You can make this call inside the component in componentDidMount since this doesn't concern anyone else

Alternatively take a look at Sagas, they observe all actions that get dispatched and decide what to do with them in a clean way.

like image 63
Ignacio Avatar answered Apr 11 '23 01:04

Ignacio