What are the benefits of entering data inside a payload key in a Redux action?

I have not found a good answer anywhere, and this seems like a meaningless agreement. In many tutorials and documentation, Redux actions are defined as follows:

{
  type: "ACTION_NAME",
  payload: {
    foo: 123,
    bar: 456,
  }
}

What is the point of the object payload? Why not just write this:

{
  type: "ACTION_NAME",
  foo: 123,
  bar: 456,
}

I use Flow, and I like to define all my types of actions as a union, without nested payloads:

type Action = {
  type: 'ACTION_NAME',
  foo: number,
  bar: number,
} | {
  type: 'ANOTHER_ACTION',
  someData: string,
} | {
  type: 'ONE_MORE_ACTION',      
  moreData: boolean,
}

I don’t need to type payloadso many times, and I get autocomplete and type checking for all my actions, so I'm not sure what I am missing.

Am I missing some of the benefits of not putting all my data inside an object payload?

+6
1

Flux,

Flux, . , , Flux , type, actionType actionId. Flux , . .

FSA .

https://github.com/acdlite/flux-standard-action

, , , Sagas, .

+5

Source: https://habr.com/ru/post/1016875/


All Articles