When is it better to call an API to update a component or just change its state by it's reducer?

Viewed 100

I have two components,in the first one i have an array of objects I get by calling the API (in y useEffect, only if the array in the store is empty, to avoid unnecessary calls). In the second one, the same array but with buttons to call the API and DELETE or POST a new object to that array in the server. My question is, is it better to create 2 actions in my second component's actions, for making the api updates, and then filter or push that object in my first component's reducer? Or just make the first component to always call the API and update itself? I don't know if it's better to update the store as mutch as u can without relying on an API call or just an API call for updating will be more smooth.

1 Answers

I would recommend you to update the first component always with API call. If so, you can be sure that data has updated properly in DB via service call and you will always get updated data from DB.

Doing more and more updates at UI and keeping them in store will leave you with dirty data after a while.

It makes your app buggy (You should keep reverting/clearing the data in store in failure scenarios. Which we might miss at some point of time) too.

Related