What is an Action?
An action is a plain object that describes something that happened in the app, with a type string and usually a payload: { type: "cart/itemAdded", payload: { id: "pen" } }. Dispatching an action is the only way to change Redux state; reducers read it and compute the new state.
Shape
type: required, conventionally"domain/eventName", for example"todos/todoAdded".payload: the data needed to perform the update.- Optional
metaanderrorfields (Flux Standard Action convention). - Must be serializable so DevTools, logging and persistence work.
Action creators
Functions that build actions: todoAdded("Buy milk"). Redux Toolkit's createSlice generates them automatically, and createAction creates standalone ones. A prepare callback can add generated values such as IDs and timestamps outside the reducer.
Naming advice
Model actions as events ("user logged in", "order placed") rather than setters ("SET_USER"). Many reducers can respond to the same event, and DevTools logs read like a story of what the user did.
Async actions
Thunks (createAsyncThunk) are functions, not plain action objects; middleware runs them, and they dispatch real actions (pending, fulfilled, rejected).
Outside Redux
useReducer uses the same idea; actions there do not need a type field by convention, but it is a good habit.
Code example
1import { createAction, createSlice, nanoid } from "@reduxjs/toolkit";23// Standalone action creator with a prepare callback (adds id + timestamp outside the reducer)4export const noteAdded = createAction("notes/noteAdded", (text: string) => ({5 payload: { id: nanoid(), text, createdAt: new Date().toISOString() },6}));78console.log(noteAdded("Hello"));9// { type: "notes/noteAdded", payload: { id: "V1StGXR8_Z5jdHi6B-myT", text: "Hello", createdAt: "..." } }1011// Slice-generated action creators12const notesSlice = createSlice({13 name: "notes",14 initialState: [] as { id: string; text: string }[],15 reducers: {16 noteDeleted: (state, action: { payload: string }) => state.filter((n) => n.id !== action.payload),17 },18 extraReducers: (b) => b.addCase(noteAdded, (state, action) => void state.push(action.payload)),19});2021export const { noteDeleted } = notesSlice.actions;22console.log(noteDeleted("abc")); // { type: "notes/noteDeleted", payload: "abc" }
Practice this question
Open it in the study app to track progress, take notes, listen to the answer and work through the rest of the React set.