@StoreChunk
Last updated
Was this helpful?
Last updated
Was this helpful?
To benefit from @ngrx-ducks'
dynamic facades, the respective class needs to be connected to NgRx . To do so, @ngrx-ducks/core
provides the decorator @StoreChunk that solves two tasks for you:
Connecting ducks and selectors with the store.
Registering the Facade as service in Angular's IoC container.
If you use @StoreChunk you are not allowed to add a constructor taking any parameters. This is by design to keep the Chunk-Class side-effect-free.
See Store registration in action at ⚡️.
Since NgRx Ducks version 12.4 a Chunk can be automatically be registered in the NgRx Store.
Since Version 13 setting feature
will cause NgRx Ducks to prefix every Action-Type for you.
A feature can have one single reducer.
@StoreFacade accepts a configuration that specifies where the Facade's reducer is about to be registered in the Store.
The configuration requires a key, where the reducer should be registered in the store. For that, you need to set feature
. The property defaults
(aka. Initial State) specifies the start value of the reducer.
The configuration shown above causes the state to have a feature counter
with an initial count
of 0.
@StoreFacade produces a Tree-Shakable-Provider. That's why you need to inject the Facade somewhere to cause the State being initialised. If the Facade is not used anywhere in your project, the coresponding state is not available either.
A feature can also have multiple reducers.
The configuration shown above causes the state to have a feature counter
that contains a slice
called simple with an initial count
of 0.
@StoreFacade
can be typed as well to ensure that defaults
are set up the correct way.
If you register a plain reducer for a feature
, the type argument is responsible for checking the defaults
.
If you register a slice
for a feature
. The type argument will check if the key of the slice
and the defaults
are correct.
The following screenshot demonstrates the occurring compiler error if a slice-key is misspelled.
The chapters , , and will teach you that a reducer function is generated out of the Facade class.
This Reducer function needs to be registered in the Store by using or .
For example, the feature-key counter
is converted to the prefix [Counter]
. You do not need to specify the Prefix manually any more .
In NgRx you would need to set up an to combine multiple reducers that belong to a feature. NgRx Ducks allows you to write a single line of code to configure that the reducer is a sibling next to other reducers of the feature.