Home Web Front-end JS Tutorial useCustomReducer Hook: A Versatile State Management Tool

useCustomReducer Hook: A Versatile State Management Tool

Dec 14, 2024 pm 05:44 PM

useCustomReducer Hook: A Versatile State Management Tool

Introduction

State management in React can be tricky, especially when dealing with complex or nested state structures. To simplify this, the useCustomReducer hook combines the power of useReducer with a flexible API for updating state in a clean, declarative way. This hook supports primitive, nested, and array states, making it suitable for a wide range of use cases.

In this article, we'll explore the useCustomReducer hook and its core methods for managing state in React applications. We'll cover the definition of the hook, its method signatures, and detailed usage examples for different types of state structures. By the end, you'll have a solid understanding of how to use the useCustomReducer hook to handle complex state in your React components.

 Table of Contents

  • Introduction
  • Table of Contents
  • Hooks Overview
  • React Component Example
  • Features
  • Definition
    • Method Definitions
  • Detailed Usage Examples
    • Managing Primitives
    • Managing Form Data
    • Managing Arrays
    • Managing Nested State
  • Why Use useCustomReducer?
  • Conclusion
  • Additional Resources

Hooks Overview

The useCustomReducer hook is a custom React hook that provides a simple and flexible way to manage complex state structures. It combines the benefits of useReducer with a clean API for updating state values. This hook is designed to handle various types of state, including primitive values, objects, arrays, and nested data structures.

Here's an overview of the useCustomReducer hook:

  • Core Methods:

    • set: Update state values directly or via a callback function.
    • reset: Revert state to its initial value.
    • merge: Merge partial updates into the existing state.
  • State Structures: - Supports primitive values (e.g., numbers, strings, booleans). - Handles object-based state structures (e.g., form data, user profiles). - Manages array-based state structures (e.g., lists, collections).

  • Type-Safe: - Fully typed using TypeScript for reliable development and error prevention.

  • Simple API: - Provides intuitive methods for updating, resetting, and merging state values. - Supports direct updates and callback functions for dynamic state changes.

import { useReducer, useCallback, useMemo } from "react";

type Primitive = boolean | string | number | Date | null | undefined;
type NestedObject = { [key: string]: Primitive | NestedObject | NestedArray };
type NestedArray = Array<Primitive | NestedObject>;

type State = Primitive | NestedObject | NestedArray;

type Action<T> =
  | { type: "SET"; payload: Partial<T> | ((prevState: T) => Partial<T>) }
  | { type: "RESET"; payload?: T }
  | { type: "MERGE"; payload: Partial<T> };

function useCustomReducer<T extends State>(initialState: T) {
  const reducer = useCallback(
    (state: T, action: Action<T>): T => {
      switch (action.type) {
        case "SET":
          const newPayload =
            typeof action.payload === "function"
              ? action.payload(state)
              : action.payload;
          if (newPayload instanceof Date) {
            return newPayload as T;
          }
          if (
            typeof state === "object" &&
            !Array.isArray(state) &&
            state !== null
          ) {
            return { ...state, ...newPayload };
          }
          return newPayload as T;
        case "RESET":
          return action.payload ?? initialState;
        case "MERGE":
          if (
            typeof state === "object" &&
            !Array.isArray(state) &&
            state !== null
          ) {
            return { ...state, ...action.payload };
          }
          return action.payload as T;
        default:
          throw new Error("Invalid action type");
      }
    },
    [initialState]
  );

  const [state, dispatch] = useReducer(reducer, initialState);

  const set = useCallback(
    (payload: Partial<T> | ((prevState: T) => Partial<T>)) =>
      dispatch({ type: "SET", payload }),
    []
  );
  const reset = useCallback(
    (payload?: T) => dispatch({ type: "RESET", payload }),
    []
  );
  const merge = useCallback(
    (payload: Partial<T>) => dispatch({ type: "MERGE", payload }),
    []
  );

  const memoizedState = useMemo(() => state, [state]);

  return [memoizedState, { set, reset, merge }] as const;
}

export default useCustomReducer;
Copy after login
Copy after login

The useCustomReducer hook is implemented using the useReducer hook from React. It defines a custom reducer function that handles different types of actions to update, reset, or merge state values. The hook provides three core methods set, reset, and merge to interact with the state. The set method can accept either an object with new state values or a callback function to compute the next state. The reset method reverts the state to its initial value, while the merge method merges partial updates into the existing state.

React Component Example

Here's an example of using the useCustomReducer hook in a React component to manage a simple counter state:

import useCustomReducer from "./use-custom-reducer";
import { faker } from "@faker-js/faker";
import { Button } from "@/components/ui/button";

export default function Use() {
  const [formValues, { set, reset, merge }] = useCustomReducer({
    name: faker.person.firstName(),
    age: faker.number.int({ min: 18, max: 99 }),
    address: {
      street: faker.location.streetAddress(),
      city: faker.location.city(),
      state: faker.location.state(),
      zip: faker.location.zipCode(),
    },
    hobbies: [faker.person.bio(), faker.person.bio(), faker.person.bio()],
  });

  const [bool, { set: setBool }] = useCustomReducer<boolean>(
    faker.datatype.boolean()
  );
  const [num, { set: setNum }] = useCustomReducer(faker.number.int());
  const [str, { set: setStr }] = useCustomReducer<string>(faker.lorem.word());
  const [date, { set: setDate }] = useCustomReducer(faker.date.recent());
  const [nil, { set: setNil }] = useCustomReducer(null);
  const [undef, { set: setUndef }] = useCustomReducer(undefined);
  const [arr, { set: setArr }] = useCustomReducer([
    faker.number.int(),
    faker.number.int(),
    faker.number.int(),
  ]);
  const [nestedArr, { set: setNestedArr }] = useCustomReducer([
    faker.number.int(),
    faker.lorem.word(),
    { three: faker.number.float() },
  ]);

  const [obj, { set: setObj }] = useCustomReducer({
    a: faker.number.int(),
    b: faker.number.int(),
    c: faker.number.int(),
  });
  const [nestedObj, { set: setNestedObj }] = useCustomReducer({
    a: faker.number.int(),
    b: faker.lorem.word(),
    c: { three: faker.number.float() },
  });

  return (
    <div className="p-4 space-y-6">
      <h1 className="text-2xl font-bold">Use</h1>
      <div className="space-x-2 space-y-2">
        <h2 className="text-lg font-semibold">Form Values</h2>
        <p className="text-gray-500">{JSON.stringify(formValues)}</p>
        <Button onClick={() => set({ name: faker.person.firstName() })}>
          Set Name
        </Button>
        <Button
          onClick={() => set((prevState) => ({ age: prevState.age - 1 }))}
        >
          Decrement Age
        </Button>
        <Button
          onClick={() => set((prevState) => ({ age: prevState.age + 1 }))}
        >
          Increment Age
        </Button>
        <Button
          onClick={() =>
            set((prevState) => ({
              address: {
                ...prevState.address,
                street: faker.location.streetAddress(),
              },
            }))
          }
        >
          Set Street
        </Button>
        <Button onClick={() => reset()}>Reset</Button>
        <Button
          onClick={() => merge({ age: faker.number.int({ min: 18, max: 99 }) })}
        >
          Merge
        </Button>
      </div>
      <hr className="border-t border-gray-300" />
      <div className="space-x-2 space-y-2">
        <h2 className="text-lg font-semibold">Boolean Value</h2>
        <p className="text-gray-500">{bool.toString()}</p>
        <Button onClick={() => setBool(faker.datatype.boolean())}>
          Set Bool
        </Button>
      </div>
      <hr className="border-t border-gray-300" />
      <div className="space-x-2 space-y-2">
        <h2 className="text-lg font-semibold">Number Value</h2>
        <p className="text-gray-500">{num.toString()}</p>
        <Button onClick={() => setNum(faker.number.int())}>Set Num</Button>
      </div>
      <hr className="border-t border-gray-300" />
      <div className="space-x-2 space-y-2">
        <h2 className="text-lg font-semibold">String Value</h2>
        <p className="text-gray-500">{str}</p>
        <Button onClick={() => setStr(faker.lorem.word())}>Set Str</Button>
      </div>
      <hr className="border-t border-gray-300" />
      <div className="space-x-2 space-y-2">
        <h2 className="text-lg font-semibold">Date Value</h2>
        <p className="text-gray-500">{JSON.stringify(date)}</p>
        <Button onClick={() => setDate(faker.date.recent())}>Set Date</Button>
        <Button onClick={() => setDate(new Date("2022-01-01"))}>
          Set Date to 2022
        </Button>
      </div>
      <hr className="border-t border-gray-300" />
      <div className="space-x-2 space-y-2">
        <h2 className="text-lg font-semibold">Nil and Undefined</h2>
        <p className="text-gray-500">{String(nil)}</p>
        <Button onClick={() => setNil(null)}>Set Nil</Button>
        <p className="text-gray-500">{String(undef)}</p>
        <Button onClick={() => setUndef(undefined)}>Set Undef</Button>
      </div>
      <hr className="border-t border-gray-300" />
      <div className="space-x-2 space-y-2">
        <h2 className="text-lg font-semibold">Array Value</h2>
        <p className="text-gray-500">{arr.toString()}</p>
        <Button
          onClick={() =>
            setArr([faker.number.int(), faker.number.int(), faker.number.int()])
          }
        >
          Set Arr
        </Button>
      </div>
      <hr className="border-t border-gray-300" />
      <div className="space-x-2 space-y-2">
        <h2 className="text-lg font-semibold">Nested Array</h2>
        <p className="text-gray-500">{JSON.stringify(nestedArr)}</p>
        <Button
          onClick={() =>
            setNestedArr([
              faker.number.int(),
              faker.lorem.word(),
              { three: faker.number.float() },
            ])
          }
        >
          Set Nested Arr
        </Button>
      </div>
      <hr className="border-t border-gray-300" />
      <div className="space-x-2 space-y-2">
        <h2 className="text-lg font-semibold">Object Value</h2>
        <p className="text-gray-500">{JSON.stringify(obj)}</p>
        <Button
          onClick={() =>
            setObj({
              a: faker.number.int(),
              b: faker.number.int(),
              c: faker.number.int(),
            })
          }
        >
          Set Obj
        </Button>
      </div>
      <hr className="border-t border-gray-300" />
      <div className="space-x-2 space-y-2">
        <h2 className="text-lg font-semibold">Nested Object</h2>
        <p className="text-gray-500">{JSON.stringify(nestedObj)}</p>
        <Button
          onClick={() =>
            setNestedObj({
              a: faker.number.int(),
              b: faker.lorem.word(),
              c: { three: faker.number.float() },
            })
          }
        >
          Set Nested Obj
        </Button>
      </div>
    </div>
  );
}
Copy after login
Copy after login

Features

  • Supports Diverse State Structures: Handles primitives, objects, arrays, and nested data structures.

  • Simple API:

    • set: Update state values directly or via a callback.
    • reset: Revert state to its initial value.
    • merge: Merge partial updates into the existing state.
  • Type-Safe: Fully typed using TypeScript for reliable development.

Definition

The useCustomReducer hook is a custom React hook for managing complex state. It provides three core methods set, reset, and merge to handle primitive, nested, and array-based state structures. Here's a breakdown of the hook and its methods:

function useCustomReducer<T extends State>(
  initialState: T
): [
  T,
  {
    set: (payload: Partial<T> | ((prevState: T) => Partial<T>)) => void;
    reset: (payload?: T) => void;
    merge: (payload: Partial<T>) => void;
  }
];
Copy after login
Copy after login

Method Definitions

  • set
    • Updates the state by replacing or partially updating its properties.
    • Accepts either:
    • An object with new state values.
    • A callback function (prevState) => Partial to compute the next state.

Example

const [state, { set }] = useCustomReducer({ count: 0 });

set((prevState) => ({ count: prevState.count + 1 }));
Copy after login
Copy after login
  • reset
    • Resets the state to the initial state or a specified value.
    • Accepts an optional payload to replace the initial state.

Example

reset(); // Resets to initial state.

reset({ name: "John", age: 25 }); // Resets to a new state.
Copy after login
Copy after login
  • merge
    • Merges partial updates into the existing state.
    • Accepts an object with partial state updates.
    • Only works for objects and nested state structures.

Example

merge({ city: "New York" }); // Adds or updates the 'city' field.
Copy after login
Copy after login

Detailed Usage Examples

The useCustomReducer hook is versatile and can be used to manage various types of state structures. Here are some examples to demonstrate its usage with different types of state:

 Managing Primitives

  • Number:
const initialState = 0;

const [count, { set, reset }] = useCustomReducer(initialState);
Copy after login
Copy after login
  • Usage:

    • Increment the Count:
    set((prevState) => prevState + 1);
    
    Copy after login
    Copy after login
    • Reset to Initial State:
    reset();
    
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    • Set a New Value:
      set(10);
    
    Copy after login
    Copy after login
  • String:

const initialState = "Hello, World!";

const [message, { set, reset }] = useCustomReducer(initialState);
Copy after login
Copy after login
  • Usage:

    • Update the String:
    import { useReducer, useCallback, useMemo } from "react";
    
    type Primitive = boolean | string | number | Date | null | undefined;
    type NestedObject = { [key: string]: Primitive | NestedObject | NestedArray };
    type NestedArray = Array<Primitive | NestedObject>;
    
    type State = Primitive | NestedObject | NestedArray;
    
    type Action<T> =
      | { type: "SET"; payload: Partial<T> | ((prevState: T) => Partial<T>) }
      | { type: "RESET"; payload?: T }
      | { type: "MERGE"; payload: Partial<T> };
    
    function useCustomReducer<T extends State>(initialState: T) {
      const reducer = useCallback(
        (state: T, action: Action<T>): T => {
          switch (action.type) {
            case "SET":
              const newPayload =
                typeof action.payload === "function"
                  ? action.payload(state)
                  : action.payload;
              if (newPayload instanceof Date) {
                return newPayload as T;
              }
              if (
                typeof state === "object" &&
                !Array.isArray(state) &&
                state !== null
              ) {
                return { ...state, ...newPayload };
              }
              return newPayload as T;
            case "RESET":
              return action.payload ?? initialState;
            case "MERGE":
              if (
                typeof state === "object" &&
                !Array.isArray(state) &&
                state !== null
              ) {
                return { ...state, ...action.payload };
              }
              return action.payload as T;
            default:
              throw new Error("Invalid action type");
          }
        },
        [initialState]
      );
    
      const [state, dispatch] = useReducer(reducer, initialState);
    
      const set = useCallback(
        (payload: Partial<T> | ((prevState: T) => Partial<T>)) =>
          dispatch({ type: "SET", payload }),
        []
      );
      const reset = useCallback(
        (payload?: T) => dispatch({ type: "RESET", payload }),
        []
      );
      const merge = useCallback(
        (payload: Partial<T>) => dispatch({ type: "MERGE", payload }),
        []
      );
    
      const memoizedState = useMemo(() => state, [state]);
    
      return [memoizedState, { set, reset, merge }] as const;
    }
    
    export default useCustomReducer;
    
    Copy after login
    Copy after login
    • Reset to Initial State:
    import useCustomReducer from "./use-custom-reducer";
    import { faker } from "@faker-js/faker";
    import { Button } from "@/components/ui/button";
    
    export default function Use() {
      const [formValues, { set, reset, merge }] = useCustomReducer({
        name: faker.person.firstName(),
        age: faker.number.int({ min: 18, max: 99 }),
        address: {
          street: faker.location.streetAddress(),
          city: faker.location.city(),
          state: faker.location.state(),
          zip: faker.location.zipCode(),
        },
        hobbies: [faker.person.bio(), faker.person.bio(), faker.person.bio()],
      });
    
      const [bool, { set: setBool }] = useCustomReducer<boolean>(
        faker.datatype.boolean()
      );
      const [num, { set: setNum }] = useCustomReducer(faker.number.int());
      const [str, { set: setStr }] = useCustomReducer<string>(faker.lorem.word());
      const [date, { set: setDate }] = useCustomReducer(faker.date.recent());
      const [nil, { set: setNil }] = useCustomReducer(null);
      const [undef, { set: setUndef }] = useCustomReducer(undefined);
      const [arr, { set: setArr }] = useCustomReducer([
        faker.number.int(),
        faker.number.int(),
        faker.number.int(),
      ]);
      const [nestedArr, { set: setNestedArr }] = useCustomReducer([
        faker.number.int(),
        faker.lorem.word(),
        { three: faker.number.float() },
      ]);
    
      const [obj, { set: setObj }] = useCustomReducer({
        a: faker.number.int(),
        b: faker.number.int(),
        c: faker.number.int(),
      });
      const [nestedObj, { set: setNestedObj }] = useCustomReducer({
        a: faker.number.int(),
        b: faker.lorem.word(),
        c: { three: faker.number.float() },
      });
    
      return (
        <div className="p-4 space-y-6">
          <h1 className="text-2xl font-bold">Use</h1>
          <div className="space-x-2 space-y-2">
            <h2 className="text-lg font-semibold">Form Values</h2>
            <p className="text-gray-500">{JSON.stringify(formValues)}</p>
            <Button onClick={() => set({ name: faker.person.firstName() })}>
              Set Name
            </Button>
            <Button
              onClick={() => set((prevState) => ({ age: prevState.age - 1 }))}
            >
              Decrement Age
            </Button>
            <Button
              onClick={() => set((prevState) => ({ age: prevState.age + 1 }))}
            >
              Increment Age
            </Button>
            <Button
              onClick={() =>
                set((prevState) => ({
                  address: {
                    ...prevState.address,
                    street: faker.location.streetAddress(),
                  },
                }))
              }
            >
              Set Street
            </Button>
            <Button onClick={() => reset()}>Reset</Button>
            <Button
              onClick={() => merge({ age: faker.number.int({ min: 18, max: 99 }) })}
            >
              Merge
            </Button>
          </div>
          <hr className="border-t border-gray-300" />
          <div className="space-x-2 space-y-2">
            <h2 className="text-lg font-semibold">Boolean Value</h2>
            <p className="text-gray-500">{bool.toString()}</p>
            <Button onClick={() => setBool(faker.datatype.boolean())}>
              Set Bool
            </Button>
          </div>
          <hr className="border-t border-gray-300" />
          <div className="space-x-2 space-y-2">
            <h2 className="text-lg font-semibold">Number Value</h2>
            <p className="text-gray-500">{num.toString()}</p>
            <Button onClick={() => setNum(faker.number.int())}>Set Num</Button>
          </div>
          <hr className="border-t border-gray-300" />
          <div className="space-x-2 space-y-2">
            <h2 className="text-lg font-semibold">String Value</h2>
            <p className="text-gray-500">{str}</p>
            <Button onClick={() => setStr(faker.lorem.word())}>Set Str</Button>
          </div>
          <hr className="border-t border-gray-300" />
          <div className="space-x-2 space-y-2">
            <h2 className="text-lg font-semibold">Date Value</h2>
            <p className="text-gray-500">{JSON.stringify(date)}</p>
            <Button onClick={() => setDate(faker.date.recent())}>Set Date</Button>
            <Button onClick={() => setDate(new Date("2022-01-01"))}>
              Set Date to 2022
            </Button>
          </div>
          <hr className="border-t border-gray-300" />
          <div className="space-x-2 space-y-2">
            <h2 className="text-lg font-semibold">Nil and Undefined</h2>
            <p className="text-gray-500">{String(nil)}</p>
            <Button onClick={() => setNil(null)}>Set Nil</Button>
            <p className="text-gray-500">{String(undef)}</p>
            <Button onClick={() => setUndef(undefined)}>Set Undef</Button>
          </div>
          <hr className="border-t border-gray-300" />
          <div className="space-x-2 space-y-2">
            <h2 className="text-lg font-semibold">Array Value</h2>
            <p className="text-gray-500">{arr.toString()}</p>
            <Button
              onClick={() =>
                setArr([faker.number.int(), faker.number.int(), faker.number.int()])
              }
            >
              Set Arr
            </Button>
          </div>
          <hr className="border-t border-gray-300" />
          <div className="space-x-2 space-y-2">
            <h2 className="text-lg font-semibold">Nested Array</h2>
            <p className="text-gray-500">{JSON.stringify(nestedArr)}</p>
            <Button
              onClick={() =>
                setNestedArr([
                  faker.number.int(),
                  faker.lorem.word(),
                  { three: faker.number.float() },
                ])
              }
            >
              Set Nested Arr
            </Button>
          </div>
          <hr className="border-t border-gray-300" />
          <div className="space-x-2 space-y-2">
            <h2 className="text-lg font-semibold">Object Value</h2>
            <p className="text-gray-500">{JSON.stringify(obj)}</p>
            <Button
              onClick={() =>
                setObj({
                  a: faker.number.int(),
                  b: faker.number.int(),
                  c: faker.number.int(),
                })
              }
            >
              Set Obj
            </Button>
          </div>
          <hr className="border-t border-gray-300" />
          <div className="space-x-2 space-y-2">
            <h2 className="text-lg font-semibold">Nested Object</h2>
            <p className="text-gray-500">{JSON.stringify(nestedObj)}</p>
            <Button
              onClick={() =>
                setNestedObj({
                  a: faker.number.int(),
                  b: faker.lorem.word(),
                  c: { three: faker.number.float() },
                })
              }
            >
              Set Nested Obj
            </Button>
          </div>
        </div>
      );
    }
    
    Copy after login
    Copy after login
  • Boolean:

function useCustomReducer<T extends State>(
  initialState: T
): [
  T,
  {
    set: (payload: Partial<T> | ((prevState: T) => Partial<T>)) => void;
    reset: (payload?: T) => void;
    merge: (payload: Partial<T>) => void;
  }
];
Copy after login
Copy after login
  • Usage:

    • Toggle the Boolean:
    const [state, { set }] = useCustomReducer({ count: 0 });
    
    set((prevState) => ({ count: prevState.count + 1 }));
    
    Copy after login
    Copy after login
    • Reset to Initial State:
    reset(); // Resets to initial state.
    
    reset({ name: "John", age: 25 }); // Resets to a new state.
    
    Copy after login
    Copy after login
    • Set a New Value:
    merge({ city: "New York" }); // Adds or updates the 'city' field.
    
    Copy after login
    Copy after login
  • Date:

const initialState = 0;

const [count, { set, reset }] = useCustomReducer(initialState);
Copy after login
Copy after login
  • Usage:

    • Update the Date:
    set((prevState) => prevState + 1);
    
    Copy after login
    Copy after login
    • Reset to Initial State:
    reset();
    
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    • Set a New Value:
      set(10);
    
    Copy after login
    Copy after login
  • Null and Undefined States:

const initialState = "Hello, World!";

const [message, { set, reset }] = useCustomReducer(initialState);
Copy after login
Copy after login
  • Usage:

    • Set a New Value:
    set("Hello, React!");
    
    Copy after login
    • Reset to Initial State:
    reset();
    
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    • Set a New Value:
    const initialState = false;
    
    const [isToggled, { set, reset }] = useCustomReducer(initialState);
    
    Copy after login

Managing Form Data

  • Initial State:
set((prevState) => !prevState);
Copy after login
  • Usage:

    • Set a New Name:
    reset();
    
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    • Update Address Partially:
      set(true);
    
    Copy after login
    • Set a New Name:
    const initialState = new Date();
    
    const [date, { set, reset }] = useCustomReducer(initialState);
    
    Copy after login
    • Update the City:
    set(new Date("2022-01-01"));
    
    Copy after login
    • Merge Additional Fields:
    reset();
    
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    • Reset to Initial State:
    set(new Date("2023-01-01"));
    
    Copy after login

Managing Arrays

  • Initial State:
const initialState: string | null = null;
const initialState: string | undefined = undefined;

const [value, { set, reset }] = useCustomReducer(initialState); // Implicitly infer the type.
const [value, { set, reset }] = useCustomReducer<string | undefined>(
  initialState
); // Explicitly define the type.
Copy after login
  • Usage:

    • Add a New Element:
    set("New Value");
    
    Copy after login
    Copy after login
    • Remove an Element:
    reset();
    
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    • Reset to Initial State:
    set("New Value");
    
    Copy after login
    Copy after login
    • Set a New Value:
    const initialState = {
      name: "John Doe",
      age: 30,
      address: {
        street: "123 Main St",
        city: "Sample City",
        state: "CA",
      },
    };
    
    const [formData, { set, reset, merge }] = useCustomReducer(initialState);
    
    Copy after login
    • Merge Additional Elements:
    set({ name: "Jane Doe" });
    
    Copy after login
    Copy after login
  • Initial State for Nested Arrays:

set((prevState) => ({
  address: {
    ...prevState.address,
    city: "New City",
  },
}));
Copy after login
  • Usage:

    • Add a New User:
    set({ name: "Jane Doe" });
    
    Copy after login
    Copy after login
    • Remove a User:
    merge({ address: { city: "New York" } });
    
    Copy after login
    • Reset to Initial State:
    merge({ phone: "123-456-7890" });
    
    Copy after login
    • Set a New Value:
    reset();
    
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    • Merge Additional Users:
    const initialState = [1, 2, 3, 4, 5];
    
    const [numbers, { set, reset, merge }] = useCustomReducer(initialState);
    
    Copy after login

Managing Nested State

  • Initial State:
set((prevState) => [...prevState, 6]);
Copy after login
  • Usage:

    • Update User's Age:
    set((prevState) => prevState.filter((item) => item !== 3));
    
    Copy after login
    • Update the City:
    reset();
    
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    • Reset to Initial State:
      set([10, 20, 30]);
    
    Copy after login
    • Set a New Value:
      merge([6, 7, 8]);
    
    Copy after login

Why Use useCustomReducer?

  • Flexible State Management:

    • Supports various state structures, making it suitable for different use cases.
    • Handles primitive, nested, and array-based states with ease.
    • Provides methods to update, reset, and merge state values.
  • Simple API:

    • Provides intuitive methods to update, reset, and merge values.
    • Supports direct updates and callback functions for dynamic state changes.
    • Offers a clean and declarative way to manage state in React components.
  • Cleaner Code:

    • Reduces boilerplate code by handling complex state structures efficiently.
    • Avoid repetitive useState declarations and directly handle complex state.
    • Manage all types of state (primitive, object, array, etc.) with one hook.
  • Type-Safe:

    • Fully typed using TypeScript for reliable development and error prevention.
  • Dynamic Updates:

    • Use the set method with functions to compute next-state dynamically.

Conclusion

The useCustomReducer hook is a powerful tool for managing complex state structures in React applications. By combining the flexibility of useReducer with a simple API for updating state, this hook simplifies state management and reduces boilerplate code. Whether you're dealing with primitive values, nested objects, or arrays, the useCustomReducer hook provides a clean and declarative way to handle state changes. Try it out in your next project and experience the benefits of versatile state management with ease.

Additional Resources

  • React Documentation
  • TypeScript Documentation
  • Faker.js Documentation

The above is the detailed content of useCustomReducer Hook: A Versatile State Management Tool. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

What should I do if I encounter garbled code printing for front-end thermal paper receipts? What should I do if I encounter garbled code printing for front-end thermal paper receipts? Apr 04, 2025 pm 02:42 PM

Frequently Asked Questions and Solutions for Front-end Thermal Paper Ticket Printing In Front-end Development, Ticket Printing is a common requirement. However, many developers are implementing...

Who gets paid more Python or JavaScript? Who gets paid more Python or JavaScript? Apr 04, 2025 am 12:09 AM

There is no absolute salary for Python and JavaScript developers, depending on skills and industry needs. 1. Python may be paid more in data science and machine learning. 2. JavaScript has great demand in front-end and full-stack development, and its salary is also considerable. 3. Influencing factors include experience, geographical location, company size and specific skills.

Demystifying JavaScript: What It Does and Why It Matters Demystifying JavaScript: What It Does and Why It Matters Apr 09, 2025 am 12:07 AM

JavaScript is the cornerstone of modern web development, and its main functions include event-driven programming, dynamic content generation and asynchronous programming. 1) Event-driven programming allows web pages to change dynamically according to user operations. 2) Dynamic content generation allows page content to be adjusted according to conditions. 3) Asynchronous programming ensures that the user interface is not blocked. JavaScript is widely used in web interaction, single-page application and server-side development, greatly improving the flexibility of user experience and cross-platform development.

How to merge array elements with the same ID into one object using JavaScript? How to merge array elements with the same ID into one object using JavaScript? Apr 04, 2025 pm 05:09 PM

How to merge array elements with the same ID into one object in JavaScript? When processing data, we often encounter the need to have the same ID...

Is JavaScript hard to learn? Is JavaScript hard to learn? Apr 03, 2025 am 12:20 AM

Learning JavaScript is not difficult, but it is challenging. 1) Understand basic concepts such as variables, data types, functions, etc. 2) Master asynchronous programming and implement it through event loops. 3) Use DOM operations and Promise to handle asynchronous requests. 4) Avoid common mistakes and use debugging techniques. 5) Optimize performance and follow best practices.

How to achieve parallax scrolling and element animation effects, like Shiseido's official website?
or:
How can we achieve the animation effect accompanied by page scrolling like Shiseido's official website? How to achieve parallax scrolling and element animation effects, like Shiseido's official website? or: How can we achieve the animation effect accompanied by page scrolling like Shiseido's official website? Apr 04, 2025 pm 05:36 PM

Discussion on the realization of parallax scrolling and element animation effects in this article will explore how to achieve similar to Shiseido official website (https://www.shiseido.co.jp/sb/wonderland/)...

The difference in console.log output result: Why are the two calls different? The difference in console.log output result: Why are the two calls different? Apr 04, 2025 pm 05:12 PM

In-depth discussion of the root causes of the difference in console.log output. This article will analyze the differences in the output results of console.log function in a piece of code and explain the reasons behind it. �...

The Evolution of JavaScript: Current Trends and Future Prospects The Evolution of JavaScript: Current Trends and Future Prospects Apr 10, 2025 am 09:33 AM

The latest trends in JavaScript include the rise of TypeScript, the popularity of modern frameworks and libraries, and the application of WebAssembly. Future prospects cover more powerful type systems, the development of server-side JavaScript, the expansion of artificial intelligence and machine learning, and the potential of IoT and edge computing.

See all articles