UseArrayState
guarahooks/use-array-stateFreeHook
Manages an array as a React state with built-in array manipulation methods.
Live preview
live · rendered by the registry
Rendered by guarahooks on their own page — this is the component running, not a screenshot of it.Install it
npx shadcn@latest add https://guarahooks.com/r/use-array-state.jsonSource
1'use client';23import { useCallback, useMemo, useState } from 'react';45export type UseArrayStateResult<T> = {6 array: T[];7 length: number;8 push: (...items: T[]) => void;9 pop: () => T | undefined;10 shift: () => T | undefined;11 unshift: (...items: T[]) => void;12 insert: (index: number, ...items: T[]) => void;13 remove: (index: number) => void;14 removeWhere: (predicate: (item: T, index: number) => boolean) => void;15 update: (index: number, item: T) => void;16 updateWhere: (17 predicate: (item: T, index: number) => boolean,18 newItem: T,19 ) => void;20 clear: () => void;21 reset: () => void;22 setValue: (newArray: T[]) => void;23 filter: (predicate: (item: T, index: number) => boolean) => void;24 sort: (compareFn?: (a: T, b: T) => number) => void;25 reverse: () => void;26 map: <U>(transform: (item: T, index: number) => U) => U[];27 find: (predicate: (item: T, index: number) => boolean) => T | undefined;28 findIndex: (predicate: (item: T, index: number) => boolean) => number;29 includes: (item: T) => boolean;30 indexOf: (item: T) => number;31 slice: (start?: number, end?: number) => T[];32 isEmpty: boolean;33 first: T | undefined;34 last: T | undefined;35};3637export interface UseArrayStateOptions<T> {38 /**39 * Initial array value40 */41 initialValue?: T[];42 /**43 * Callback called whenever the array changes44 */45 onChange?: (array: T[]) => void;46}4748/**49 * Manages an array as a React state with built-in array manipulation methods50 * @param options - Configuration options for the hook51 * @returns Object with array state and manipulation methods52 */53export function useArrayState<T>(54 options: UseArrayStateOptions<T> = {},55): UseArrayStateResult<T> {56 const { initialValue = [], onChange } = options;5758 const [array, setArray] = useState<T[]>(initialValue);59 const initialArray = useMemo(() => [...initialValue], [initialValue]);6061 // Helper to update array and call onChange62 const updateArray = useCallback(63 (newArray: T[]) => {64 setArray(newArray);65 if (onChange) {66 onChange(newArray);67 }68 },69 [onChange],70 );7172 // Array manipulation methods73 const push = useCallback(74 (...items: T[]) => {75 setArray((prev) => {76 const newArray = [...prev, ...items];77 if (onChange) onChange(newArray);78 return newArray;79 });80 },81 [onChange],82 );8384 const pop = useCallback(() => {85 let poppedItem: T | undefined;86 setArray((prev) => {87 if (prev.length === 0) return prev;88// … truncated
Files it writes
More from guarahooks
All 100 items| Component | Registry | Kind | Access | Installs | Command |
|---|---|---|---|---|---|
| UseAbortControlleruse-abort-controller | guarahooks | Hooks | Free | no deps | |
| UseAxiosuse-axios | guarahooks | Hooks | Free | no deps | |
| UseBatteryStatususe-battery-status | guarahooks | Hooks | Free | no deps | |
| UseBetterAuthuse-better-auth | guarahooks | Hooks | Free | no deps | |
| UseClickOutsideuse-click-outside | guarahooks | Hooks | Free | no deps | |
| UseConfirmuse-confirm | guarahooks | Hooks | Free | no deps | |
| UseCookieuse-cookie | guarahooks | Hooks | Free | no deps | |
| UseCopyToClipboarduse-copy-to-clipboard | guarahooks | Hooks | Free | no deps |
