Action Objects: type and payload
What you'll learn: How to structure action objects with a type string and optional payload for passing data to your reducer.
The Standard Action Shape
In the previous lesson, you learned that actions are instruction objects. Now let's look at the convention that makes actions predictable across all React and Redux codebases.
An action object typically has two properties:
type: A string that identifies what happened (required)payload: Any data needed to perform the update (optional)
This shape isn't enforced by React, but it's the industry standard — like a handshake everyone agrees on.
Why This Convention?
Think of actions like letters in the mail. The type is the letter's purpose ("invoice," "invitation," "reminder"), and the payload is the content inside the envelope. The reducer reads the type to decide what to do, then uses the payload to do it.
// Action with just a type
{ type: 'INCREMENT' }
// Action with type and payload
{ type: 'ADD_ITEM', payload: { id: 5, name: 'Widget' } }
// Another example
{ type: 'UPDATE_USER', payload: { email: 'new@example.com' } }
In your reducer's switch statement, you check action.type to determine which case to execute, then use action.payload to access the data:
switch (action.type) {
case 'ADD_ITEM':
return { ...state, items: [...state.items, action.payload] };
case 'UPDATE_USER':
return { ...state, user: { ...state.user, ...action.payload } };
default:
return state;
}
Key Takeaway: Action objects follow the { type: string, payload?: any } convention — type identifies the action, payload carries the data. This standard makes reducers predictable and your code easier to understand.