DEV Community

Cover image for Unleashing the Power of Reducers: How React Developers Can Revolutionize Their State Management!
Ibrahim Bagalwa
Ibrahim Bagalwa

Posted on • Updated on

Unleashing the Power of Reducers: How React Developers Can Revolutionize Their State Management!

Are you tired of managing state in your React application? Do you find it difficult to keep track of changes to your application's state and wish there was an easier way to handle it?
Look no further than useReducer hook in React!

Reducers are pure functions that take the current state and an action as arguments, and return a new state. The state in Redux is typically represented as a plain JavaScript object, and actions are plain JavaScript objects that describe changes to that state. A reducer is responsible for updating the state based on the action that was dispatched.

Let's take a simple example of a counter app. We have a state object that contains a single property called count, and we want to be able to increment or decrement that count.
Here's what our initial state might look like:

const defaultState = {
    count: 0
}
Enter fullscreen mode Exit fullscreen mode

Next, we define our reducer function:

   function counterReducer(state = defaultState, action) {
     switch (action.type) {
       case "INCREMENT":
         return { ...state, count: state.count + 1 };
       case "DECREMENT":
         return { ...state, count: state.count - 1 };
       default:
        return state;
     }
  }
Enter fullscreen mode Exit fullscreen mode

In this example, our reducer function takes two arguments: the current state and the action. We use a switch statement to determine what action was dispatched, and we return a new state object based on the action. In the case of "INCREMENT", we create a new state object using the spread operator (...) to copy all of the properties of the current state object, and then we increment the count property by 1. Similarly, in the case of "DECREMENT", we create a new state object and decrement the count property by 1.

Now that we have defined our reducer function, we can use it in our React component. We can use the useReducer hook from React to create a state object and a dispatch function. The state object will hold the current state, and the dispatch function will be used to update the state by dispatching actions. Here's an example of how we might use the useReducer hook in our counter app:

import { useReducer } from "react";

function Counter() {
 const [state, dispatch] =useReducer(counterReducer,defaultState);
 const handleIncrement = () => {
    dispatch({ type: "INCREMENT" });
  };

 const handleDecrement = () => {
    dispatch({ type: "DECREMENT" });
  };

 return (
    <div>
      <h1>Count: {state.count}</h1>
      <button onClick={handleIncrement}>Increment</button>
      <button onClick={handleDecrement}>Decrement</button>
    </div>
  );
}
Enter fullscreen mode Exit fullscreen mode

In this example, we use the useReducer hook to create a state object and a dispatch function based on our counterReducer function and defaultState. We also define two functions to handle the "INCREMENT" and "DECREMENT" actions, which we can use to update the state by dispatching actions. Finally, we render a simple UI that displays the current count and two buttons to increment or decrement the count.

The reducers are an important part of state management in React, and they provide a predictable way to update state based on actions. By defining a reducer function, we can update our state in a way that is easy to reason about and test. If you are using Redux for state management in your React application, understanding reducers is essential.

Top comments (0)