In React, we use useState to manage data inside a component.
But when the state becomes more complex and involves multiple actions, managing everything with useState can become difficult.
React provides another Hook called useReducer to make state management easier.
1. What is useReducer?
useReducer is a React Hook used to manage state in a component.
It is useful when we have multiple actions that change the same state.
In simple words, useReducer helps us manage state by using a function to decide how the state should change.
2. Syntax of useReducer
First, import useReducer from React.
import { useReducer } from "react";
The basic syntax is:
const [state, dispatch] = useReducer(reducer, initialState);
Let's understand each part:
- state: Stores the current value.
- dispatch: Used to send an action to the reducer.
- reducer: A function that decides how the state should change.
- initialState: The starting value of the state.
3. Simple Counter Example
Let's create a counter with three actions:
- Increase the count.
- Decrease the count.
- Reset the count.
import { useReducer } from "react";
// Reducer function
function reducer(state, action) {
if (action.type === "INCREMENT") {
return state + 1;
}
if (action.type === "DECREMENT") {
return state - 1;
}
if (action.type === "RESET") {
return 0;
}
return state;
}
function App() {
// Starting value
const initialState = 0;
const [state, dispatch] = useReducer(reducer, initialState);
return (
<div>
<h2>Count: {state}</h2>
<button onClick={() => dispatch({ type: "INCREMENT" })}>
Increase
</button>
<button onClick={() => dispatch({ type: "DECREMENT" })}>
Decrease
</button>
<button onClick={() => dispatch({ type: "RESET" })}>
Reset
</button>
</div>
);
}
export default App;
4. How Does It Work?
Let's understand what happens when we click the Increase button.
Step 1: Initial State
const initialState = 0;
Our counter starts with the value 0.
Step 2: Dispatch an Action
dispatch({ type: "INCREMENT" });
When we click the Increase button, dispatch sends an action called INCREMENT.
Step 3: Reducer Receives the Action
function reducer(state, action) {
if (action.type === "INCREMENT") {
return state + 1;
}
}
The reducer checks the action type and increases the current state by 1.
Step 4: UI Updates
The reducer returns the new state.
React updates the component, and the new count appears on the screen.
This is how useReducer works!
5. What is the Difference Between useState and useReducer?
Both are React Hooks used for managing state, but they are useful in different situations.
| useState | useReducer |
|---|---|
| Simple state management | More complex state management |
| Uses a setter function | Uses a dispatch function |
| State update logic is usually inside event handlers | State update logic is inside the reducer function |
| Good for simple values | Good for multiple related actions |
Example:
-
useState: Updating a username or showing a message. -
useReducer: Managing a counter with multiple actions or handling several related state changes.
6. Important Points to Remember
-
useReduceris a React Hook. - It is used to manage component state.
-
reduceris a function that calculates the next state. -
dispatchsends an action to the reducer. -
initialStatedefines the starting value. - The reducer should return a new state instead of directly modifying the existing state.
Top comments (0)