Reputation: 828
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 :)
Upvotes: 5
Views: 295
Reputation: 1272
I would use the ProjectStore
to store both _projects
and _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 MessageStore
and 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.
Upvotes: 4