Open In App

ReactJS useReducer Hook

Last Updated : 12 Feb, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

The useReducer hook is an alternative to the useState hook that is preferred when you have complex state logic. It is useful when the state transitions depend on previous state values or when you need to handle actions that can update the state differently.

Syntax

const [state, dispatch] = useReducer(reducer, initialState);
  • reducer: A function that defines how the state should be updated based on the action. It takes two parameters: the current state and the action.
  • initialState: The initial value of the state.
  • Statestates: The current state returned from the useReducer hook.
  • dispatch: A function used to send an action to the reducer to update the state.

Implementing the useReducer hook

1. Basic Counter using useReducer

A common example of using useReducer is managing the state of a counter with actions to increment and decrement the value.

JavaScript
import React, { useReducer } from 'react';
const counterReducer = (state, action) => {
    switch (action.type) {
        case 'INCREMENT':
            return { count: state.count + 1 };
        case 'DECREMENT':
            return { count: state.count - 1 };
        default:
            return state;
    }
};
function Counter() {
    const [state, dispatch] = useReducer(counterReducer, { count: 0 });
    return (
        <div>
            <p>Count: {state.count}</p>
            <button onClick={() => dispatch({ type: 'INCREMENT' })}>Increment</button>
            <button onClick={() => dispatch({ type: 'DECREMENT' })}>Decrement</button>
        </div>
    );
}
export default Counter;

Output

Animationkk

Counter app

In this example

  • counterReducer: A reducer function that handles two actions, INCREMENT and DECREMENT, updating the count value accordingly.
  • dispatch: This function triggers the reducer with the action type (INCREMENT or DECREMENT), which updates the state.

2. Managing Complex State with Multiple Actions

For more complex state management, you can use useReducer to handle actions that affect different parts of the state, such as managing a form or multiple values at once.

JavaScript
import React, { useReducer } from 'react';
const initialState = { name: '', age: 0, submitted: false };
const formReducer = (state, action) => {
    switch (action.type) {
        case 'SET_NAME':
            return { ...state, name: action.payload };
        case 'SET_AGE':
            return { ...state, age: action.payload };
        case 'SUBMIT':
            return { ...state, submitted: true };
        default:
            return state;
    }
};
function Form() {
    const [state, dispatch] = useReducer(formReducer, initialState);

    const handleSubmit = () => {
        dispatch({ type: 'SUBMIT' });
    };
    return (
        <div>
            <input
                type="text"
                value={state.name}
                onChange={(e) => dispatch({ type: 'SET_NAME', payload: e.target.value })}
                placeholder="Enter your name"
            />
            <input
                type="number"
                value={state.age}
                onChange={(e) => dispatch({ type: 'SET_AGE', payload: e.target.value })}
                placeholder="Enter your age"
            />
            <button onClick={handleSubmit}>Submit</button>
            {state.submitted && <p>Form Submitted!</p>}
        </div>
    );
}
export default Form;

Output

In this example

  • formReducer: The reducer function handles three actions: SET_NAME, SET_AGE, and SUBMIT. Each action updates the corresponding part of the state.
  • dispatch: Dispatches actions to modify the state (e.g., setting the name or age, or marking the form as submitted).

Interesting Things About the useReducer Hook

  • Manages Complex State Logic: Unlike useState, which is ideal for simple state updates, useReducer is more suited for handling complex state logic that involves multiple sub-values or intricate state transitions.
  • Improves Performance in Large Applications: Since useReducer allows state updates based on dispatch actions, it helps optimize performance by reducing unnecessary re-renders, especially in large-scale applications.
  • Better State Management for Predictability: With useReducer, state updates follow a predictable pattern where actions dictate the changes, making it easier to debug and manage state changes systematically.

When to Use useReducer

You should use useReducer when

  • The state logic is complex and involves multiple sub-values or requires sophisticated updates.
  • You need to manage state transitions in a predictable manner (such as when working with forms or handling multiple actions).
  • You have multiple state variables that depend on each other and need to be updated together.
  • The logic for updating the state is not just a simple assignment but involves computations, conditions, or complex updates.

useReducer vs useState

Both useReducer and useState manage state in React, but they serve different purposes:

  • useState is used for simple state updates where you only need to store one value (e.g., numbers, strings).
  • useReducer is used for more complex state logic, such as managing state transitions involving multiple sub-values.

Performance Considerations

While useReducer provides structure and clarity, it can be more complex and may add some overhead if used unnecessarily. Here are some performance considerations:

  • Avoid overuse: If your state management is simple, useState might be more efficient. Use useReducer for complex or shared state logic.
  • Optimal re-renders: Since useReducer uses a single state object and dispatches actions, it can help avoid unnecessary re-renders, making it more efficient in larger applications with complex interactions.


Next Article

Similar Reads