Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ngrx actions with optional payload

I'm working with Angular 8 and NgRx 8, is it possible create an action with optional payload? I'm trying to do something like:

export const MyAction = createAction('[Action ] MyAction', props<{ prop?: string }>());

but unfortunately doesn't work. Could you help me?

like image 242
Vinni Avatar asked Dec 14 '25 06:12

Vinni


2 Answers

Based on this post there is an optional payload definition.

Example:

export const FetchPlayersAction = createAction(
  '[Players.List] FETCH_PLAYERS',
  (payload: ListingOptions = {}) => payload, // defined optional payload
);
like image 82
Franky238 Avatar answered Dec 15 '25 23:12

Franky238


Based on your example, you can set a default value, and then it's optional:

export const MyAction = createAction(
  '[Action ] MyAction',
  (prop: string = 'My default value') => ({ prop })
);
like image 20
Sebastian Denis Avatar answered Dec 15 '25 23:12

Sebastian Denis



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!