Clean β’ Professional
In React, the useReducer hook provides a powerful alternative to useState for managing complex or nested state logic. Itβs particularly useful when your component has multiple related state variables or when state updates depend on previous state values.
While useState is perfect for simple state updates, useReducer brings predictable state transitions and centralized logic, making your code cleaner and easier to maintain.
The useReducer hook works similarly to reducers in Redux:
dispatch to send actions to the reducer, updating state.Syntax:
const [state, dispatch] = useReducer(reducer, initialState);
state β Current state valuedispatch β Function to send actions to the reducerreducer β Function that calculates the next stateinitialState β Initial state valueExample:
import React, { useReducer } from "react";
// Reducer function
function reducer(state, action) {
switch (action.type) {
case "increment":
return { count: state.count + 1 };
case "decrement":
return { count: state.count - 1 };
case "reset":
return { count: 0 };
default:
return state;
}
}
function Counter() {
const [state, dispatch] = useReducer(reducer, { count: 0 });
return (
<div>
<h1>Count: {state.count}</h1>
<button onClick={() => dispatch({ type: "increment" })}>+</button>
<button onClick={() => dispatch({ type: "decrement" })}>-</button>
<button onClick={() => dispatch({ type: "reset" })}>Reset</button>
</div>
);
}
export default Counter;
useReducer is ideal when:
useState calls.Example β Multiple State Management:
const initialState = { count: 0, text: "" };
function reducer(state, action) {
switch (action.type) {
case "increment":
return { ...state, count: state.count + 1 };
case "setText":
return { ...state, text: action.payload };
default:
return state;
}
}
function App() {
const [state, dispatch] = useReducer(reducer, initialState);
return (
<div>
<h2>Count: {state.count}</h2>
<inputtype="text"
value={state.text}
onChange={(e) => dispatch({ type: "setText", payload: e.target.value })}
/>
<button onClick={() => dispatch({ type: "increment" })}>Increment</button>
</div>
);
}

useState becomes hard to manage.| Feature | useState | useReducer |
|---|---|---|
| Best For | Simple state | Complex or multiple related state |
| State Updates | Directly using setter | Through actions dispatched to reducer |
| State Logic Location | Inline in component | Centralized in reducer function |
| Scaling | Harder for complex state | Easier to maintain and scale |