Home >Web Front-end >JS Tutorial >useCustomReducer Hook: A Versatile State Management Tool
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.
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:
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;
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.
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> ); }
Supports Diverse State Structures: Handles primitives, objects, arrays, and nested data structures.
Simple API:
Type-Safe: Fully typed using TypeScript for reliable development.
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; } ];
const [state, { set }] = useCustomReducer({ count: 0 }); set((prevState) => ({ count: prevState.count + 1 }));
reset(); // Resets to initial state. reset({ name: "John", age: 25 }); // Resets to a new state.
merge({ city: "New York" }); // Adds or updates the 'city' field.
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:
const initialState = 0; const [count, { set, reset }] = useCustomReducer(initialState);
Usage:
set((prevState) => prevState + 1);
reset();
set(10);
String:
const initialState = "Hello, World!"; const [message, { set, reset }] = useCustomReducer(initialState);
Usage:
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;
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> ); }
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; } ];
Usage:
const [state, { set }] = useCustomReducer({ count: 0 }); set((prevState) => ({ count: prevState.count + 1 }));
reset(); // Resets to initial state. reset({ name: "John", age: 25 }); // Resets to a new state.
merge({ city: "New York" }); // Adds or updates the 'city' field.
Date:
const initialState = 0; const [count, { set, reset }] = useCustomReducer(initialState);
Usage:
set((prevState) => prevState + 1);
reset();
set(10);
Null and Undefined States:
const initialState = "Hello, World!"; const [message, { set, reset }] = useCustomReducer(initialState);
Usage:
set("Hello, React!");
reset();
const initialState = false; const [isToggled, { set, reset }] = useCustomReducer(initialState);
set((prevState) => !prevState);
Usage:
reset();
set(true);
const initialState = new Date(); const [date, { set, reset }] = useCustomReducer(initialState);
set(new Date("2022-01-01"));
reset();
set(new Date("2023-01-01"));
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.
Usage:
set("New Value");
reset();
set("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);
set({ name: "Jane Doe" });
Initial State for Nested Arrays:
set((prevState) => ({ address: { ...prevState.address, city: "New City", }, }));
Usage:
set({ name: "Jane Doe" });
merge({ address: { city: "New York" } });
merge({ phone: "123-456-7890" });
reset();
const initialState = [1, 2, 3, 4, 5]; const [numbers, { set, reset, merge }] = useCustomReducer(initialState);
set((prevState) => [...prevState, 6]);
Usage:
set((prevState) => prevState.filter((item) => item !== 3));
reset();
set([10, 20, 30]);
merge([6, 7, 8]);
Flexible State Management:
Simple API:
Cleaner Code:
Type-Safe:
Dynamic Updates:
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.
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!