useReducer State Reducer

Last updated: 2026-11-02

useReducer State Reducer

useReducer concentrates the question "how does the state change?" into one pure reducer function: dispatching an action triggers it and the reducer computes the new state from that action. When there are many state transitions, complicated logic, or several values that must change together, it is clearer and more testable than a pile of useState calls. The reducer is a plain function, so it can be exercised on its own without rendering anything at all.

GroupEffects & Performance
KindHook
SinceReact 16.8

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

📝 Syntax

function reducer(state, action) {
  switch (action.type) {
    case 'increment': return { count: state.count + 1 }
    case 'reset':     return { count: 0 }
    default:          return state    // an unknown action returns the same state
  }
}

const [state, dispatch] = useReducer(reducer, { count: 0 })
dispatch({ type: 'increment' })

⚙️ Parameters

reducerThe pure function (state, action) => newState: no side effects, no direct mutation of the state, and it must return a new object.
initialArgThe initial state; a third argument can pass a lazy initializer init(initialArg).

Returns:Returns [state, dispatch], the current state and the dispatch function. The reference of dispatch never changes, so it is safe to put in a dependency array or to pass to a child component.

▶ Example

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

Output:

The three buttons dispatch the inc, dec and reset actions, the count is computed by the reducer and the UI updates in real time.

💡 Key Points

💡 Tip The dispatch function has a stable reference, so passing it down to a deep child needs no further useCallback.
ℹ️ Info The reducer must be a pure function: fetching data or writing to localStorage belong in an event handler or a useEffect.
⚠️ Warning Mutating the state directly (state.count++) is the classic mistake — always return a new object.
More case studies coming soon.

📚 Related Tutorials

❓ FAQ

Q How do I choose between useReducer and useState?
A Use useState for an independent and simple value; use useReducer when several values change together, the update logic is complicated, or one step depends on the previous one.
Q Can the reducer send a request?
A No, a reducer must be a pure function. Put the request in an event handler or an effect and dispatch the result when it arrives.
Q Can I read the new state right after dispatch?
A No. Dispatch only schedules a re-render, so the state in the current closure is still the old value; watch it with a useEffect when you need to react to it.
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%

🙏 帮我们做得更好

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

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