Search code examples
angularreduxngrxngrx-store-4.0

NGRX angular payload or no payload


In the NGRX library (https://ngrx.io/), when creating an action that includes data, the examples show a payload argument provided to contain this data. Is there a reason I cannot just provide the payload as parameters? The documentation and all examples I can find use a payload but there is no real explanation for why. For example:

export class CreateEmployeeScheduleError implements Action {
readonly type = CREATE_EMPLOYEE_SCHEDULE_ERROR;
constructor(public payload: {
        error: string,
        requests: ScheduleCreateRequest[],
        requestsRemaining: number
}) {}
}

could be written as:

export class CreateEmployeeScheduleError implements Action {
readonly type = CREATE_EMPLOYEE_SCHEDULE_ERROR;
constructor(public error: string,
        public requests: ScheduleCreateRequest[],
        public requestsRemaining: number) {}
}

This eliminates the need to get the action.payload, as well as the need for a helper function (map(toPayload)) and is similar to how I've seen other redux implementation frameworks (NGXS). Is there any reason why I should not do it as the latter?

This would change effects and reducers to be simpler as well.


Solution

  • You don't have to use payload it's just a convention. See Let’s have a chat about Actions and Action Creators within NgRx

    From ngrx.io

    The interface has a single property, the type, represented as a string. The type property is for describing the action that will be dispatched in your application. The value of the type comes in the form of [Source] Event and is used to provide a context of what category of action it is, and where an action was dispatched from. You add properties to an action to provide additional context or metadata for an action. The most common property is the payload, which adds any associated data needed for the action.