Home:ALL Converter>React + Redux App: Calling API through a Redux action Vs Calling an API directly

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

Ask Time:2018-04-11T12:05:46         Author:Abhilash Reddy

Json Formatter

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?

Author:Abhilash Reddy,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/49766326/react-redux-app-calling-api-through-a-redux-action-vs-calling-an-api-directly
Ignacio :

It depends on what kind of call you're trying to make, and who's concern it is.\nHere are a few cases:\n\n\nIs 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.\nThis doesn't have to be stored in Redux's store.\nIs 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.\nIs 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\n\n\nAlternatively take a look at Sagas, they observe all actions that get dispatched and decide what to do with them in a clean way. ",
2018-04-11T05:11:38
Prasun :

The accepted answer quite well explains the scenario where from API call can be initiated. For better user experience, we always show some spinner or busy sign to inform the user that a request is being made and it has not finished yet. It may happen that API response is not mutating the state, but to let the user know some task is going in the background, we usually update store (for global access) or state (for component level access) with value like isFetching or anything meaningful.\n\nSo, it depends on the developer, whether he/she want to show some busy sign or silently perform the API request. Moreover, if they want to show busy sign then, they should decide which part of the application should be aware of the API call. If it is restricted to the component level only, then no need to make the call in actions, otherwise, for global level, yes it should be inside action.",
2018-04-11T16:15:26
yy