React 中的状态管理可能很棘手,尤其是在处理复杂或嵌套的状态结构时。为了简化这一点,useCustomReducer 挂钩将 useReducer 的强大功能与灵活的 API 结合起来,以干净、声明的方式更新状态。该钩子支持原始状态、嵌套状态和数组状态,使其适用于广泛的用例。
在本文中,我们将探索 useCustomReducer 钩子及其用于管理 React 应用程序状态的核心方法。我们将介绍钩子的定义、其方法签名以及不同类型状态结构的详细使用示例。最后,您将深入了解如何使用 useCustomReducer 钩子来处理 React 组件中的复杂状态。
useCustomReducer 钩子是一个自定义的 React 钩子,它提供了一种简单而灵活的方法来管理复杂的状态结构。它将 useReducer 的优点与用于更新状态值的干净 API 结合起来。该钩子旨在处理各种类型的状态,包括原始值、对象、数组和嵌套数据结构。
以下是 useCustomReducer 钩子的概述:
核心方法:
状态结构: - 支持原始值(例如数字、字符串、布尔值)。 - 处理基于对象的状态结构(例如表单数据、用户配置文件)。 - 管理基于数组的状态结构(例如列表、集合)。
类型安全: - 使用 TypeScript 进行完全类型化,以实现可靠的开发和错误预防。
简单 API: - 提供更新、重置和合并状态值的直观方法。 - 支持动态状态变化的直接更新和回调函数。
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;
useCustomReducer 钩子是使用 React 中的 useReducer 钩子实现的。它定义了一个自定义减速器函数,用于处理不同类型的操作以更新、重置或合并状态值。该钩子提供了三个核心方法 set、reset 和 merge 来与状态交互。 set 方法可以接受具有新状态值的对象或回调函数来计算下一个状态。重置方法将状态恢复为其初始值,而合并方法将部分更新合并到现有状态。
这是在 React 组件中使用 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> ); }
支持多种状态结构:处理基元、对象、数组和嵌套数据结构。
简单的API:
类型安全:使用 TypeScript 进行完全类型化,以实现可靠的开发。
useCustomReducer 钩子是一个用于管理复杂状态的自定义 React 钩子。它提供了三个核心方法 set、reset 和 merge 来处理原始、嵌套和基于数组的状态结构。以下是钩子及其方法的详细说明:
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.
useCustomReducer 钩子用途广泛,可用于管理各种类型的状态结构。以下是一些示例来演示其在不同类型状态下的用法:
const initialState = 0; const [count, { set, reset }] = useCustomReducer(initialState);
用法:
set((prevState) => prevState + 1);
reset();
set(10);
字符串:
const initialState = "Hello, World!"; const [message, { set, reset }] = useCustomReducer(initialState);
用法:
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> ); }
布尔值:
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.
日期:
const initialState = 0; const [count, { set, reset }] = useCustomReducer(initialState);
用法:
set((prevState) => prevState + 1);
reset();
set(10);
空和未定义状态:
const initialState = "Hello, World!"; const [message, { set, reset }] = useCustomReducer(initialState);
用法:
set("Hello, React!");
reset();
const initialState = false; const [isToggled, { set, reset }] = useCustomReducer(initialState);
set((prevState) => !prevState);
用法:
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.
用法:
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" });
嵌套数组的初始状态:
set((prevState) => ({ address: { ...prevState.address, city: "New City", }, }));
用法:
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]);
用法:
set((prevState) => prevState.filter((item) => item !== 3));
reset();
set([10, 20, 30]);
merge([6, 7, 8]);
灵活的状态管理:
简单 API:
更简洁的代码:
类型安全:
动态更新:
useCustomReducer 钩子是一个强大的工具,用于管理 React 应用程序中的复杂状态结构。通过将 useReducer 的灵活性与用于更新状态的简单 API 相结合,该挂钩简化了状态管理并减少了样板代码。无论您处理原始值、嵌套对象还是数组,useCustomReducer 挂钩都提供了一种干净且声明性的方式来处理状态更改。在您的下一个项目中尝试一下,轻松体验多功能状态管理的好处。
以上是useCustomReducer Hook:多功能状态管理工具的详细内容。更多信息请关注PHP中文网其他相关文章!