I want to create an Action with an array as an argument, but this doesn't type check:
export const action_organizations_available = createAction(
CoreActionTypes.ORGANIZATIONS_AVAILABLE,
props<Organization[]>()
);
I get props(): "arrays are not allowed in action creators"
.
It works fine when the type is not an array.
Using props<{ organizations: Organization[] }>
works, but I wonder if it's the best practice.
What is the best practice when expecting an array as an argument in an action?
Actions are used in many ways in NgRx. Actions are the inputs and outputs of systems in NgRx. Actions help you to understand how events are handled in our application. The Action interface An Action in NgRx is formed by a simple interface: interface Action { type: string; }
NgRx packages are mainly divided into these categories State Store - RxJS used for global state management for Angular apps. Effects - Side effect model for @ngrx/store. Router Store - connect the Angular Router to @ngrx/store for Bindings. Entity – To manage record collections, it will use Entity State adapter.
The conventional action class can handle the union type of the class type as it is, but in the case of a function, it is necessary to combine the return type of the function. It is NgRx's union function to assist it. Pass all Action Creators to the union function and declare its return value ** without exporting **.
The NgRX Store models a state as a single and simple JavaScript object inside the Store. The state is immutable or read-only. This means that there is no direct Store API to change the state object inside the Store. There are other means of updating the state that I will be discussing soon.
You have to wrap it into an object:
export const action_organizations_available = createAction(
CoreActionTypes.ORGANIZATIONS_AVAILABLE,
props<{ organizations: Organization[] }>()
);
EDIT: it's now a rule "prefer-inline-action-props" in the NgRx ESLint Plugin.
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