Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When to create a new Flux Store

I was wondering what the best practice or convention is when creating stores in Flux when working with an API

Let's say we have a list of 'projects', the API call would eventually fill a Store called ProjectStore in _projects

Then, when a user selects a project, you want to load the Project specific data. Would you add this to the same ProjectStore as _activeProject, or create a seperate Store for it?

Same for when you load the Todo's within that Project. It would make sense to put these in a TodoStore, but what about a specific Todo within the Todos within the Project?

I hope the above makes sense :)

like image 734
Hyra Avatar asked Sep 29 '22 17:09

Hyra


1 Answers

I would use the ProjectStoreto store both _projectsand _activeProject.

I have done as a rule in my React projects to create a new store for each model. For instance: Let's say that I have an application that serves messages and accounts. I would then have a MessageStoreand an AccountStore. Then use these accordingly to their domain. E.g., when I want to get some messages from the backend (through an API), I store them in the MessageStore. The same goes for the AccountStore.

As your project becomes larger and larger, you would probably have to reconsider refactoring something into a new store. For instance, if you want to add advertisement to your site, you could create an AdsStore.

My rule of thumb is to try to separate stores by their domain, but don't make the structure more complex than it has to be.

Hope that helped.

like image 168
magnudae Avatar answered Oct 02 '22 16:10

magnudae