Action Design

Last updated: 2026-11-02

Action Design Action Design

An action is a plain object describing what happened: its type is the identity, usually an upper-case constant, and its payload carries the data. Good action design turns the reducer into something as readable as a switch directory, and it makes the flow of state traceable and replayable in the DevTools. Because actions are plain data, they survive serialization, which is exactly what makes logging, debugging and time-travel possible.

GroupEffects & Performance
KindPattern

ℹ️ Info The editor transpiles JSX to JavaScript and renders the real UI in a sandbox; components mount automatically — no manual render call needed.

📝 Syntax

// { type: 'todo/added', payload: 'buy milk' }
const addTodo = text => ({ type: 'todo/added', payload: text })
const toggleTodo = id => ({ type: 'todo/toggled', payload: id })

dispatch(addTodo('buy milk'))    // the action creator builds the object

Returns:No return value; this entry describes an encoding pattern rather than a function.

▶ Example

The example runs out of the box: it auto-renders when expanded, and edits take effect instantly.

Output:

Type some text and click "Add" to create a todo; clicking an item toggles its done state (with a line through it) — every change is driven by one of the two actions, added and toggled.

💡 Key Points

💡 Tip An action creator such as addTodo(text) gathers the shape of the action in one place, so callers never have to assemble the object by hand.
ℹ️ Info Name the type after the domain and the action, such as todo/added; it reads well in the DevTools and makes filtering by prefix easy.
⚠️ Warning Keep only serializable data in an action (the payload), never a callback or a component instance.
More case studies coming soon.

📚 Related Tutorials

❓ FAQ

Q Are action creators mandatory?
A No, dispatching an object works as well; but with many actions or a complex shape a creator keeps the structure consistent and avoids typos.
Q Can the payload hold a function?
A It should not. An action is a pure data description, and functions would break serializability and the ability to replay the state.
Q Do action types have to be defined in one place?
A In a small project you can export the constants from the top of the reducer file; in a modular project define them next to their domain and prefix them to avoid collisions.
Web-Tutorial.com

Web-Tutorial Tech Team

A team of developers maintaining programming tutorials. Each tutorial is written and reviewed by developers with expertise in that field. We work to keep our content accurate and reliable — if you spot an issue, please let us know.

100%

🙏 帮我们做得更好

我们是刚上线的编程教程站,几个人的小团队,精力有限。页面虽经检查,难免还有疏漏——链接失效、排版错乱、内容有误、语言生硬……

如果您发现了,麻烦告诉我们,我们会在收到反馈后第一时间进行修复,再次感谢您的光临 🙏