createDuck
Last updated
Was this helpful?
Last updated
Was this helpful?
In Redux Actions are used to set up the communication between components.
If you are not familiar with NgRx's Actions, we highly recommend to check their documentation (see ) first, before starting with Ducks.
Using createDuck
allows you to create a fully typed action that also contains the action dispatcher. A Duck can dispatch itself. It expects at least one parameter, the action type. The sample below demonstrates how the property greet
becomes a duck, which means that greet
now is a method that additionally contains a function dispatch.
Please note that createDuck.dispatch
only works inside a class being annoted with @StoreFacade. You will receive an error in the console if you accidently forget@StoreFacade
.
Often an action transports a payload. To define an action dispatcher with a payload, createDuck
accepts a second parameter being able to set up a strictly typed action creator.
The second parameter needs to be filled with dispatch<TPayload>
where TPaylod
defines the type of the payload. In the component the dispatch-method now expects a parameter with the defined payload-type.
A duck can also contain a case-reducer.
According to the shape of the case-reducer the action dispatcher is generated accordingly.
If a case-reducer proceeds a payload the dispatcher expects a payload.
If a case-reducer has no payload, the dispatcher expects no payload.
One last question remains. How does the Store know about the case-reducers that has been added to a facade? Please read the section to learn how the reducer-function is generated by @ngrx-ducks and how the reducer gets finally bound to the Store.