useReducer Hook
Learn how to manage component state using the useReducer Hook, reducer functions, actions, and dispatch in React.
1. What Is the useReducer Hook?
The useReducer Hook is a built-in React Hook used to manage state through a reducer function. It is useful when state updates involve multiple actions or more complex logic.
Instead of updating state directly through a setter function, a component dispatches an action. The reducer receives the current state and the action, then returns the next state.
2. Syntax of useReducer
The basic syntax is:
const [state, dispatch] = useReducer(
reducer,
initialState
);
- reducer: A function that calculates the next state.
- initialState: The initial state value.
- state: The current state returned by the Hook.
- dispatch: The function used to send an action to the reducer.
3. How useReducer Works
The component dispatches an action when a user performs an operation. React passes the current state and action to the reducer and uses the returned state for the next render.
4. Example: Counter Using useReducer
In this example, the counter supports three actions: increase, decrease, and reset.
Example Code
import { useReducer } from "react";
function reducer(state, action) {
switch (action.type) {
case "increase":
return state + 1;
case "decrease":
return state - 1;
case "reset":
return 0;
default:
return state;
}
}
function App() {
const [count, dispatch] = useReducer(
reducer,
0
);
return (
<div>
<h2>Counter Application</h2>
<p>Count: {count}</p>
<button onClick={() =>
dispatch({ type: "increase" })
}>
Increase
</button>
<button onClick={() =>
dispatch({ type: "decrease" })
}>
Decrease
</button>
<button onClick={() =>
dispatch({ type: "reset" })
}>
Reset
</button>
</div>
);
}
export default App;
Expected Output
Counter Application
Initial value: Count: 0
After clicking Increase: Count: 1
After clicking Increase again: Count: 2
After clicking Decrease: Count: 1
After clicking Reset: Count: 0
action.type using a
switch statement. The dispatch function sends an action,
and the reducer returns the new counter value.
5. Managing Object State with useReducer
The useReducer Hook can also manage objects containing multiple related values, such as a student's name and course.
Example Code
import { useReducer } from "react";
const initialState = {
name: "Samadhan",
course: "React"
};
function reducer(state, action) {
switch (action.type) {
case "changeName":
return {
...state,
name: "Samiksha"
};
case "changeCourse":
return {
...state,
course: "Angular"
};
default:
return state;
}
}
function App() {
const [student, dispatch] = useReducer(
reducer,
initialState
);
return (
<div>
<h2>Student Information</h2>
<p>Name: {student.name}</p>
<p>Course: {student.course}</p>
<button onClick={() =>
dispatch({ type: "changeName" })
}>
Change Name
</button>
<button onClick={() =>
dispatch({ type: "changeCourse" })
}>
Change Course
</button>
</div>
);
}
export default App;
Expected Output
Initially:
Name: SamadhanCourse: React
After clicking Change Name:
Name: SamikshaAfter clicking Change Course:
Course: Angular...state preserves the other
properties while returning a new object.
6. Difference Between useState and useReducer
- useState: Suitable for simple state values and straightforward updates.
- useReducer: Useful when multiple actions determine how state changes.
- useState: Updates state through a setter function.
- useReducer: Dispatches actions to a reducer function.
- Both Hooks can update state and cause React to re-render the component when the state changes.
7. Important Points About useReducer
- Import useReducer from React.
- The reducer receives the current state and an action.
- Dispatch sends an action to the reducer.
- The reducer must return the next state.
- Avoid directly modifying objects or arrays in state.
- Use a default case to handle unexpected action types.
- Keep side effects outside the reducer function.
8. Practice Tasks
Complete the following exercises:
- Create a counter using useReducer with Increase and Decrease actions.
- Add a Reset action to the counter.
- Create a reducer that changes a student's name.
- Manage student name and course using an object state.
- Explain the difference between dispatch and a useState setter function.
- Write a reducer with a default case for unknown actions.