How to use a Reducer with hooks in React?

Dec 30, 2025

Leave a message

Hey there! If you're into React and looking to up your game with reducers, you're in the right place. I'm not just some random tech enthusiast; I'm actually a reducer supplier. Yeah, I know, it sounds a bit niche, but it's a pretty cool gig. So, let's dive into how to use a reducer with hooks in React.

What's a Reducer Anyway?

First things first, a reducer is a pure function that takes the current state and an action as arguments and returns a new state. It's like a little state - changing machine. In a nutshell, it helps you manage the state of your application in a predictable way.

Let's say you're building a simple to - do list app. You have a list of tasks, and you want to be able to add, remove, or mark tasks as completed. All these actions can be handled by a reducer.

For example, here's a basic reducer function for our to - do list:

const todoReducer = (state, action) => {
    switch (action.type) {
        case 'ADD_TASK':
            return [...state, action.task];
        case 'REMOVE_TASK':
            return state.filter(task => task.id!== action.id);
        case 'COMPLETE_TASK':
            return state.map(task =>
                task.id === action.id? {...task, completed: true } : task
            );
        default:
            return state;
    }
};

In this code, the todoReducer function takes the current state (which is an array of tasks) and an action. Depending on the action.type, it returns a new state.

Using Reducer with Hooks in React

Now, let's talk about how to use this reducer with hooks in React. React provides a hook called useReducer that makes it super easy to integrate reducers into your functional components.

Here's how you can use it in a component:

import React, { useReducer } from'react';

const todoReducer = (state, action) => {
    switch (action.type) {
        case 'ADD_TASK':
            return [...state, action.task];
        case 'REMOVE_TASK':
            return state.filter(task => task.id!== action.id);
        case 'COMPLETE_TASK':
            return state.map(task =>
                task.id === action.id? {...task, completed: true } : task
            );
        default:
            return state;
    }
};

const initialState = [];

const TodoApp = () => {
    const [todos, dispatch] = useReducer(todoReducer, initialState);

    const addTask = (task) => {
        dispatch({ type: 'ADD_TASK', task });
    };

    const removeTask = (id) => {
        dispatch({ type: 'REMOVE_TASK', id });
    };

    const completeTask = (id) => {
        dispatch({ type: 'COMPLETE_TASK', id });
    };

    return (
        <div>
            <h1>Todo List</h1>
            <input
                type="text"
                placeholder="Add a task"
                onKeyDown={(e) => {
                    if (e.key === 'Enter') {
                        addTask({ id: Date.now(), text: e.target.value, completed: false });
                        e.target.value = '';
                    }
                }}
            />
            <ul>
                {todos.map(task => (
                    <li key={task.id}>
                        <span style={{ textDecoration: task.completed? 'line - through' : 'none' }}>
                            {task.text}
                        </span>
                        <button onClick={() => removeTask(task.id)}>Remove</button>
                        <button onClick={() => completeTask(task.id)}>
                            {task.completed? 'Uncomplete' : 'Complete'}
                        </button>
                    </li>
                ))}
            </ul>
        </div>
    );
};

export default TodoApp;

In this TodoApp component, we use useReducer to manage the state of our to - do list. The useReducer hook returns the current state (in this case, todos) and a dispatch function. The dispatch function is used to send actions to the reducer.

When the user types a task in the input field and presses Enter, the addTask function is called, which dispatches an action of type 'ADD_TASK'. The reducer then takes the current state and adds the new task to it.

Types of Reducers in the Real World

As a reducer supplier, I deal with different types of reducers. In the plumbing world (this is a different kind of reducer, but still cool), there are some common types.

One type is the Buttweld Concentric Reducer. It's used to connect two pipes of different diameters in a straight line. Another type is the Threaded Reducer, which is used when you need to connect pipes with threaded ends. And then there's the Eccentric Weld Reducer, which is used when the centerlines of the two pipes need to be offset.

It's interesting how the concept of a reducer exists in different fields, but the core idea of taking something and changing it into something else remains the same.

Benefits of Using Reducers with Hooks

Using reducers with hooks in React has several benefits.

Eccentric Weld ReducerEccentric Weld Reducer

First of all, it makes your code more predictable. Since a reducer is a pure function, it always returns the same output for the same input. This means that you can easily test your reducers and understand how the state is changing in your application.

Secondly, it helps with complex state management. In larger applications, the state can get really complicated. Using reducers allows you to break down the state management into smaller, more manageable pieces.

Lastly, it improves the readability of your code. When you use reducers, it's clear what actions can be taken and how they affect the state. This makes it easier for other developers (or even future you) to understand and maintain the code.

Challenges and How to Overcome Them

Of course, using reducers with hooks isn't all sunshine and rainbows. There can be some challenges.

One challenge is understanding the flow of actions and state changes. When you have multiple actions and complex state, it can be hard to keep track of what's going on. To overcome this, you can use tools like React DevTools, which allow you to inspect the state and actions in your application.

Another challenge is performance. If your reducer function is doing a lot of calculations or has a large amount of data to process, it can slow down your application. To address this, you can optimize your reducer function by using techniques like memoization.

Conclusion

Well, that's a wrap on how to use a reducer with hooks in React. It's a powerful combination that can take your React applications to the next level. Whether you're building a small to - do list app or a large - scale enterprise application, reducers can help you manage your state more effectively.

If you're looking for high - quality reducers for your plumbing projects (or other industrial applications), don't hesitate to reach out. I'm here to help you find the right reducer for your needs. Just drop me a message, and we can start discussing your requirements.

References

  • React Official Documentation
  • JavaScript Design Patterns books
  • Online tutorials on React state management
Michael Brown
Michael Brown
Michael is a quality control expert in Hebei Huayang Steel Pipe Co., Ltd. He strictly adheres to international standards such as ISO 9001, ISO 14001, and ISO 45001, ensuring that every steel pipe produced meets high - quality requirements. His rigorous work attitude has won the trust of customers.
Send Inquiry