Skip to Content
HooksuseArray

useArray

useArray manages array state and exposes immutable helpers for common collection updates like push, move, update, remove, clear, reset, and shuffle.

Live Example

Stateful list actions

This demo shows a real array-state flow where the hook owns the list and updates it directly.

Collection state

Array actions

3 items

This is a real stateful use case: the hook owns the list and exposes immutable helpers for common collection updates.

ReactHooksDocs

Import

import { useArray } from "react-rsc-kit/client";

Signature

const array = useArray<T>(initialValue?);

Parameters

NameTypeDefaultDescription
initialValueT[] | () => T[][]Initial array value. Supports lazy initialization.

Returns

KeyDescription
valueThe current array state.
lengthCurrent array length.
isEmptyWhether the array is empty.
setValueDirect useState setter for custom updates.
pushAppends one or more items.
filterKeeps only the items that match a predicate.
removeAtRemoves a single item by index. Supports negative indices.
removeValueRemoves all items that are Object.is-equal to the supplied value.
updateReplaces one item by index. Supports negative indices.
moveMoves one item to a different index. Supports negative indices.
clearRemoves all items.
resetRestores the original mount-time array snapshot.
shuffleRandomizes the array order immutably.

Usage

"use client"; import { useArray } from "react-rsc-kit/client"; export function TodoTags() { const { value, push, removeValue, move, reset } = useArray(["react", "typescript", "docs"]); return ( <div> <p>{value.join(", ")}</p> <button type="button" onClick={() => push("hooks")}> Add hooks </button> <button type="button" onClick={() => removeValue("docs")}> Remove docs </button> <button type="button" onClick={() => move(0, -1)}> Move first to end </button> <button type="button" onClick={reset}> Reset </button> </div> ); }

Notes

  • useArray now owns the array state instead of mutating a passed-in array.
  • All helper actions update state immutably.
  • reset always restores the original mount-time value, not later prop changes.
  • Use setValue when you need custom logic such as object updates by id.
Last updated on