Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple stores in ngrx

Tags:

angular

ngrx

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.

like image 723
DJClayworth Avatar asked Mar 21 '18 14:03

DJClayworth


People also ask

Can you have multiple stores NgRx?

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.

Which type of application has multiple stores NgRx Flux Redux RxJS?

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 is NgRx store stored?

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.

What is a store in NgRx?

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.


1 Answers

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.

like image 140
bygrace Avatar answered Nov 03 '22 08:11

bygrace