简体   繁体   中英

Flux stores changing other stores

I'm creating an app with Flux and React, which is kinda of a game... and this app has 3 stores:

GameStore -> contains the game status like finished, how many times the player won, and this stuff

CounterStore -> Is a countdown store that is self triggered every 1 second, and the counter component updates itself.

BlockStore -> this is related to the game componentes, which are kinda of blocks that the user can interact on.

So, whenever I click on a Block, I trigger an action and get it on the BlockStore. The BlockStore does some magic and identifies if the block clicked was correct, if it was, it updates the GameStore (with how many times the user clicked on the correct block) and updates the CounterStore (which increases the timer for the user).

Also, whenever the CounterStore gets to 0, it also changes the GameStore to set that the user has lost.

This feels totally wrong, my stores they are connected somehow... Each action changes almost all of the stores, but some actions depends on the store result, to change the other stores (that's confusing :S).

I want to get a opinion whether I'm doing it in the right way, maybe I should place everything in one store (which will make it hard to maintain), or remove some stores and treat some stuff as a component state directly (the counter for example).

I'm lost here.

Pulled from https://facebook.github.io/flux/docs/overview.html :

Stores contain the application state and logic. Their role is somewhat similar to a model in a traditional MVC, but they manage the state of many objects — they do not represent a single record of data like ORM models do. Nor are they the same as Backbone's collections. More than simply managing a collection of ORM-style objects, stores manage the application state for a particular domain within the application .

Your GameStore should should not be holding information for each block. The BlockStore should be holding the blocks and the data pertaining to the individual blocks. Since you have other data that describes the overall state of the game, I would recommend having a second store that is dedicated to holding that state (ie. a StateStore that would hold the counter, in-play/finished, score, etc).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM