Why use Redux to request the API in React?

Asked

Viewed 579 times

1

I started to see React well before learning how to use Redux and always requested in the API using a Fetch or Axios in the component itself.

Today I already venture into the world of Redux and come across people making API calls through Redux using frameworks such as redux-saga and redux-thunk, creating more complex, larger code with multiple reducers and actions to make a request. Thing I could do with only 4 or 5 lines using fetch or axios.

I would like to know if you really should use Redux to make API calls and why I should use it to improve my applications

1 answer

1


Danilo, I usually use Redux when I need to use a query response to the API for more than one component and avoid the props Drill. When the components are fully separated then Redux is an excellent way to "share the state" with the other components. I recommend a reading from the blog Rocketseat.

  • I thought that too, but I came across a situation that I think would not look very cool with it, for example: I have a component listing, in these components have a button to bookmark the item, when favoriting the item makes a request to API stating that I favored it. , the way I do I put a setState on the component to mark that the item was favorited and only that component was going to update. If I use Redux for this, when I preferred it, all the items in the list would propagate this change because of Redux and I would have to check if the change is for this item

  • Danilo, Redux would actually be perfect for this. Only the components you need would hear the change. Since the idea of Redux is a global state, only the item you have bookmarked would look like this. I use Redux with Saga in an application here and it works just like that.

  • 1

    To understand the benefits of store in the React applications, it would be good to read flux architecture in Portuguese Flux

Browser other questions tagged

You are not signed in. Login or sign up in order to post.