Emanuel Bodin
Emanuel Bodin

Reputation: 21

How should redux store be used in large react applications?

I'm currently building an e-commerce react application, where users can sell pre owned stuff to each other. A typical workflow from a user perspective for creating and managing ads can look like this: A user creates an Ad -> The Ad is uploaded to a database -> The user fetches his ads from a database and views them in a list -> He decide to change something in an Ad, clicks it and gets a form with the ad data (fetched from database) that can be edited.

My problem here is that I'm really confused about what to keep in redux? The purpose of redux is to simplify data flow, data that is needed in multiple components should be stored there. But if I always fetch the data i need from database (as for example when a user fetch a list of his ads or the ad data that can be edited), isn't redux just an unnecessary step then?

I want to be consistent throughout my code about how I'm fetching data. Right now I've been using redux to store all my data fetched from database, keeping all my API calls in different actions. The data fetching however get really troublesome when it comes to fetching data from the database that then should be editable by the user. To make fetched data editable, it has to be stored in the components local state, so storing the data both in redux and local state feels very inconvenient.

I've searched the web for specific guidelines for the but I'm not getting any wiser. To be honest I don't think I've understood how to use redux properly. I would be really grateful for some advice on this matter.

Upvotes: 2

Views: 119

Answers (1)

Muhammad Haseeb
Muhammad Haseeb

Reputation: 1341

Primarily its used for application state management. You need to consider your usecase and yourself simple questions i.e

  • Do you need that state for rest of the applications?
  • Will other components get affected by these states?
  • How often your data will change?
  • Are there other users using the same data and can update it?
  • Do you need to cache the data to prevent refetching?

The view built on top of it is a reflection of that state, but does not have to exclusively use that state container for everything it does.

For example, in your case, you will need to keep your api's responses into redux state if you want to pass that state to other components and you dont want to do the same API call again and again.

But, if your data changes so often and you need refreshed data everytime you should not keep it in the redux. Otherwise you will end up having stale data on different screens.

Note : There could also be other possibilities that can be consider but this is some very basic info that I thought will be useful for you to take a decision

Upvotes: 1

Related Questions