🎣 useReducer
21 Jan, 2026
On this page
- TS/React docs: https://github.com/typescript-cheatsheets/react-typescript-cheatsheet/blob/master/README.md#hooks
Example Jump to heading
import { useReducer } from 'react'
interface State {
count: number
}
type Action = { type: 'increment' } | { type: 'decrement' } | { type: 'reset'; payload: number }
const initialState: State = { count: 0 }
const reducer = (state: State, action: Action): State => {
switch (action.type) {
case 'increment':
return { count: state.count + 1 }
case 'decrement':
return { count: state.count - 1 }
case 'reset':
return { count: action.payload }
}
}
export const Counter = () => {
const [state, dispatch] = useReducer(reducer, initialState)
return (
<>
Count: {state.count}
<button onClick={() => dispatch({ type: 'decrement' })}>-</button>
<button onClick={() => dispatch({ type: 'increment' })}>+</button>
<button onClick={() => dispatch({ type: 'reset', payload: 0 })}>Reset</button>
</>
)
}
With lazy initialization Jump to heading
const init = (initialCount: number): State => {
return { count: initialCount }
}
const Counter = ({ initialCount }: { initialCount: number }) => {
const [state, dispatch] = useReducer(reducer, initialCount, init)
// ...
}
useImmer Jump to heading
For complex stuff, useImmer is a great option
← Back home