Can someone please explain what exactly a Payload
is in Redux
context? In layman terms please, the technical term wasn't useful. Hence still the confusion.
What I understand is that Payload
is the actual data that is transmitted over the network. Does this mean, Payload
of an action
in Redux
context means that the data that is passed as a parameter while an action is emitted to change the Redux
state
?
The second one is “payload”, Payload stores the additional information about what happened. It is not a compulsion to include “payload” in the object. It is entirely up to you but we should always try to pass only the necessary information to the action object and try to keep it as light as possible.
Payload is what is keyed ( the key value pairs ) in your actions and passed around between reducers in your redux application. For example - const someAction = { type: "Test", payload: {user: "Test User", age: 25}, }
Actions are the only source of information for the store as per Redux official documentation. It carries a payload of information from your application to store. As discussed earlier, actions are plain JavaScript object that must have a type attribute to indicate the type of action performed.
While action types allow you tell your reducer what action it should take, the payload is the data that your reducer will use to update the state. This lesson shows you how to pass an action payload along with your action type to update the state.
Payload is a non-official, community accepted (de facto) naming convention for the property that holds the actual data in a Redux action object.
The official documentation only states that a Redux action has to be a plain object and needs a string action type:
A plain object describing the change that makes sense for your application. ... Actions must have a type field that indicates the type of action being performed. Types can be defined as constants and imported from another module. It's better to use strings for type than Symbols because strings are serializable. Other than type, the structure of an action object is really up to you. If you're interested, check out Flux Standard Action for recommendations on how actions could be constructed.
Lots of things are not standardized in Redux so you have maximal flexibility to do the things in your own way, but since most of us don't want to come up with a custom solution to every little everyday detail, the community tends to establish best practices.
To separate this type from regular data the payload
property is used. Now, on what should go into payload
and what should be on the same level with it is debatable, but a popular standard (recommended by the official docs too) is the Flux Standard Action which states that among the official requirements you may add a payload
, error
, and meta
property. Here the payload is defined as:
The optional
payload
property MAY be any type of value. It represents the payload of the action. Any information about the action that is not thetype
or status of the action should be part of thepayload
field. By convention, iferror
istrue
, the payload SHOULD be an error object.
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