19

I have a few ajax requests that are not directly manipulating my apps state. In a react/redux application is it necessary (or is there any benefit) to dispatch an action for these ajax requests instead of just sending an ajax request directly in the component?

To simplify my scenario, I essentially have a list of objects on my redux state. I am using a form to post a new object to the database, upon successful post I am redirecting to the list page where a GET request is sent and the list is fetched and the state is updated.

The AJAX call to post a new object is not directly manipulating my state.

The team I am working with is going through the full 3 step redux async steps ex: 'FETCH_REQUESTED', 'FETCH_SUCCESS', 'FETCH_FAIL' along with the respective reducers for all the AJAX requests and it's a big hassle to add more and the reducers don't seem to make sense.

Nakilon
  • 34,866
  • 14
  • 107
  • 142
jmancherje
  • 6,439
  • 8
  • 36
  • 56

2 Answers2

38

You can absolutely send AJAX calls directly from components!

Redux is a tool for making shared state globally available to multiple components, and changed in predictable way. In any case where you don’t find this necessary, don’t do it.

Keeping AJAX calls in action creators is convenient when different components make the same API requests and then change the state in similar ways. It is also convenient if you want to avoid firing off a request when there is already some cached data available, and you want to keep such checks in a single place rather than scattered across the components.

That said Redux is only concerned with how global state is updated, and if you just need to make an AJAX request from some component, you don’t have to write an action creator or a reducer for it unless you find it convenient.

Generally saying Redux (and Flux) is what you might consider refactoring your code to when you have many complicated components; not what you should start every component with. You can use only the parts of it that you need (e.g. just the synchronous stuff), or even avoid it altogether in some cases (e.g. a collapsible panel doesn’t have to store its state in a store). Only use it when you understand the specific benefits it gives you in a particular situation, never “just in case” or because it is popular.

See also my answer to “How do dispatch a Redux action with a timeout?”

To address your specific example, you might want to use Redux for this if you use the benefits Redux gives you: maybe you dispatch an action to update the form optimistically and display the new list right away, and merge it with the fetched list when it is available so that the interaction appears instantaneous. That is the use case for async action creators. If you’re not looking at this kind of UX complexity, I’m not sure Redux is necessary at all.

Community
  • 1
  • 1
Dan Abramov
  • 264,556
  • 84
  • 409
  • 511
  • to follow up with Dan Abramov's answer - you might want to use - https://github.com/gavriguy/react-indie its a componet (i just made) specifically made for independent components (like widgets) that you don't want to connect to the global state by design. – Gavriguy Apr 15 '16 at 02:06
  • @DanAbramov just a question: if you don't place the collapsible panel state into the store, you won't benefit from time travel and webpack hot reload, right? – Buzinas Apr 26 '16 at 18:04
  • Yes but how often do you spend time debugging a collapsible panel logic? ;-) – Dan Abramov Apr 27 '16 at 11:42
0

Try using below link

https://github.com/sskyy/redux-task

It can help you to manage AJAX request state without those verbose actions and reducers.

dhruv jadia
  • 1,684
  • 2
  • 15
  • 28
sskyy
  • 1