0

I was using axios for api calls in class component of React. And used Redux to store mostly login details.

So started learning about Redux-thunk and now I am confused as to where to do API calls? Is doing api calls from view side not good? Do i need to use redux-thunk?

sidmas13
  • 3
  • 2
  • 1
    I suggest you read this post from Dan Abramov: https://stackoverflow.com/questions/35411423/how-to-dispatch-a-redux-action-with-a-timeout/35415559#35415559 – Muhammed B. Aydemir Aug 25 '20 at 13:19

1 Answers1

0

You use redux-thunk when you require async actions for your reducers.

Calling an API would be a good example of that. Doing API calls is something the view would do, anyways (e.g., otherwise you'd have them in your components, which is "view", too). The API is then doing the business logic and is only providing "view data". Your view might also want to track the state of the async action (e.g. "list is loading...", "failed to load because xyz", "{list of users when loaded}")

You don't need to use redux-thunk, but chances are at some point you re-implemented it, anyways (I did that and then switched). I suggest you simply use @reduxjs/toolkit (They have a good documentation site)

It really eases up working with actions, thunks, async thunks and reducers in general.

Torben
  • 193
  • 11