Using Reducer in React
Introduction
A component can become more complex over time as more state variables and event handler functions are added. In such cases, we can use the useReducer() hook to manage state in a more organized and predictable way.
Without using Reducer
Let’s create a component called Server. The following code is written without using a reducer.
import { useState } from "react";
export default function Server() {
const [status, setStatus] = useState<string>("Loading");
function changeToOnline() {
setStatus("Online");
}
function changeToOffline() {
setStatus("Offline");
}
function changeToLoading() {
setStatus("Loading");
}
return (
<>
<h1>Server Status: {status}</h1>
<button onClick={changeToOnline}>Online</button>
<button onClick={changeToOffline}>Offline</button>
<button onClick={changeToLoading}>Loading</button>
</>
);
}Nothing new just using useState() to manage the state of the server status. We have three functions to change the status to "Online", "Offline", and "Loading".
You can extract these functions into outside of the component by using reducer. Let's see how we can do that.
Using Reducer
Here is the code using useReducer() to manage the state of the server status.
import { useReducer } from "react";
type Action = {
type: "CHANGE_TO_ONLINE" | "CHANGE_TO_OFFLINE" | "CHANGE_TO_LOADING";
};
function statusReducer(state: string, action: Action): string {
switch (action.type) {
case "CHANGE_TO_ONLINE":
return "Online";
case "CHANGE_TO_OFFLINE":
return "Offline";
case "CHANGE_TO_LOADING":
return "Loading";
default:
return state;
}
}
const initialStatus = "Loading";
export default function Server() {
const [status, dispatch] = useReducer(statusReducer, initialStatus);
return (
<>
<h1>Server Status: {status}</h1>
<button onClick={() => dispatch({ type: "CHANGE_TO_ONLINE" })}>
Online
</button>
<button onClick={() => dispatch({ type: "CHANGE_TO_OFFLINE" })}>
Offline
</button>
<button onClick={() => dispatch({ type: "CHANGE_TO_LOADING" })}>
Loading
</button>
</>
);
}To create a reducer, we first define an action type that describes the possible actions that can be dispatched. In this case, we have three actions: “CHANGE_TO_ONLINE”, “CHANGE_TO_OFFLINE”, and “CHANGE_TO_LOADING”.
Get Ye Min Ko’s stories in your inbox
Join Medium for free to get updates from this writer.
Then we define a reducer function called statusReducer that takes the current state and an action as arguments. The reducer function uses a switch statement to determine how to update the state based on the action type.
In Server component, we use the useReducer() hook to initialize the state and get the dispatch function. The dispatch function is used to send actions to the reducer, which updates the state accordingly.
It seems like a lot of code for a simple state management, but as the component grows and more states are added, using a reducer can help keep the code organized and maintainable.
Carrying Payload
You can also carry a payload with the action to make the reducer more flexible. For example, if we want to change the status to a custom value, we can modify the action type and the reducer function as follows:
import { useReducer } from "react";
type Action = {
type: "UPDATE_STATUS";
payload: string;
};
function statusReducer(state: string, action: Action): string {
switch (action.type) {
case "UPDATE_STATUS":
return action.payload;
default:
return state;
}
}
const initialStatus = "Loading";
export default function Server() {
const [status, dispatch] = useReducer(statusReducer, initialStatus);
return (
<>
<h1>Server Status: {status}</h1>
<button
onClick={() => dispatch({ type: "UPDATE_STATUS", payload: "Online" })}
>
Online
</button>
<button
onClick={() => dispatch({ type: "UPDATE_STATUS", payload: "Offline" })}
>
Offline
</button>
<button
onClick={() => dispatch({ type: "UPDATE_STATUS", payload: "Loading" })}
>
Loading
</button>
</>
);
}As you can see, we have modified the action type to “UPDATE_STATUS” and added a payload property to carry the new status value. The reducer function now updates the state based on the payload value.
Conclusion
Reducer is a powerful tool for managing state in React applications, especially when dealing with complex state logic or multiple state variables. By using useReducer(), we can keep our code organized, maintainable, and more predictable.







