useReducer
In React, useReducer is a tool that helps you manage state in a way that’s more suitable for complex scenarios. Instead of using separate useState calls for individual pieces of state, useReducer uses a single function called a “reducer” to handle state changes based on actions.
Here’s a simple analogy to understand it better:
Imagine you have a “counter” with buttons to increase and decrease the count. With useState, you might do this:
import React, { useState } from 'react';
const MyComponent = () => {
const [count, setCount] = useState(0);
const increment = () => {
setCount(count + 1);
};
const decrement = () => {
setCount(count - 1);
};
return (
<div>
<p>Count: {count}</p>
<button onClick={increment}>Increment</button>
<button onClick={decrement}>Decrement</button>
</div>
);
};
Now, with useReducer, we can manage this state more efficiently:
import React, { useReducer } from 'react';
// Reducer function: Handles state updates based on actions
const reducer = (state, action) => {
switch (action.type) {
case 'INCREMENT':
return { count: state.count + 1 };
case 'DECREMENT':
return { count: state.count - 1 };
default:
return state;
}
};
const initialState = { count: 0 };
const MyComponent = () => {
const [state, dispatch] = useReducer(reducer, initialState);
return (
<div>
<p>Count: {state.count}</p>
<button onClick={() => dispatch({ type: 'INCREMENT' })}>Increment</button>
<button onClick={() => dispatch({ type: 'DECREMENT' })}>Decrement</button>
</div>
);
};
In this version, we use useReducer instead of multiple useState calls. The reducer function takes the current state and an action as input and returns the updated state based on the action type. We then use useReducer to manage the state and get a dispatch function, which we use to send actions to the reducer.
So, useReducer helps us organize and handle state in a more centralized way, making it easier to manage more complex state logic in our components. It can be especially useful when dealing with multiple actions that need to update the same state.