Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use an array as argument in createAction for @ngrx/store

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?

like image 360
Qortex Avatar asked Nov 17 '19 17:11

Qortex


People also ask

How to use actions in ngrx?

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; }

What is @ngrx package in angular?

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.

How to combine the return type of a function in ngrx?

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 **.

How do I change the state of the ngrx store?

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.


1 Answers

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.

like image 144
timdeschryver Avatar answered Sep 19 '22 17:09

timdeschryver