I am writing an enterprise-scale application with Angular and ngrx. The intention is to use Flux and ngrx throughout. For reuse and separability we require (at least) two state stores that do not interact with each other. But we do need both stores to be active at the same time, and potentially accessed from the same components.
Ngrx seems to be predicated on the assumption that there will only ever be one Store at once. Is there an approach that will allow me to have multiple Store objects (templated of course with different State objects), and have them both loaded and active at the same time?
I'm aware that 'best practice' recommends combining the stores into one. That's not viable here unless there is an entirely novel approach.
For reuse and separability we require (at least) two state stores that do not interact with each other. But we do need both stores to be active at the same time, and potentially accessed from the same components. Ngrx seems to be predicated on the assumption that there will only ever be one Store at once.
Where Flux applications traditionally have multiple stores, Redux applications have only one global, read-only application state. This state is calculated by "reducing" over a collection or stream of actions that update it in controlled ways.
Where Does NgRx Store Data? NgRx stores the application state in an RxJS observable inside an Angular service called Store. At the same time, this service implements the Observable interface.
Ngrx is a group of Angular libraries for reactive extensions. Ngrx/Store implements the Redux pattern using the well-known RxJS observables of Angular 2. It provides several advantages by simplifying your application state to plain objects, enforcing unidirectional data flow, and more.
I'd suggest setting up two feature states. Here are the relevant docs: https://github.com/ngrx/platform/blob/v5.2.0/docs/store/api.md#feature-module-state-composition
Though it isn't the same thing as having two separate stores it is the same for most practical purposes. The feature states are loaded when the module that imports StoreModule.forFeature('featureName', reducers)
is loaded. You could do this lazy or eager. Each feature state will have access to root state so you can put common state on root state that both can access. Feature states should never reference each-other as they may not be loaded and that would negate the reason for having them.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With