Reducer Patterns

Last updated: 2026-11-02

Reducer Patterns Reducer Patterns

A few idiomatic patterns keep reducers tidy: lazy initialization through the third argument, immutable updates of object and array state, computing derived values while rendering instead of storing them, and resetting a whole state tree with a reset action or a changed key. Mastering these keeps even a complex state machine clean. The shared principle is that the reducer stays a pure function, while everything impure — network calls, timers, storage — happens in the event handlers or effects around it.

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

// lazy initialization: the initial state is computed from an argument
const [state, dispatch] = useReducer(reducer, props, init)

// reset the whole state tree
case 'reset': return init(props)

// the immutable update templates for arrays
[...state, item]                    // add
state.filter(i => i.id !== id)      // remove
state.map(i => i.id === id ? { ...i, done: true } : i)  // edit

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:

"Next" accumulates the trail, "Back" steps back one and "Reset" returns to the start at once — the example demonstrates lazy initialization and the reset pattern.

💡 Key Points

💡 Tip The third argument performs the lazy initialization: use it when the initial state depends on props or is expensive to compute, and it runs only when the state is first created.
ℹ️ Info To reset the state of a component you can also change the key of the outer element, <Wizard key={resetId} />, and the whole subtree is rebuilt with fresh state.
⚠️ Warning Derived values such as the length of a list or whether everything is checked are computed while rendering, not stored in the state where two sources of truth would compete.
More case studies coming soon.

📚 Related Tutorials

❓ FAQ

Q When should I use the third argument of useReducer?
A When the initial state has to be computed from props or is expensive to build: useReducer(reducer, props, init) and init runs only the first time.
Q How do I reset the whole reducer state?
A Define a reset action that returns init(...); or give the component a new key, which makes React throw the old state tree away.
Q What if updating nested objects gets too verbose?
A Spread every level with {...a, ...b} or split the state along its paths; for large states consider immer or the normalized shape used by state libraries.
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%

🙏 帮我们做得更好

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

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