Docs
useKeyedArray
Manage an array of objects with unique keys using the useKeyedArray hook.
Installation
pnpm dlx shadcn@latest add https://usekit.kiron.dev/k/use-keyed-array
Usage
"use client"
import { useKeyedArray } from "@/hooks/use-keyed-array"
export function Component() {
const items = [{ name: "Item 1" }, { name: "Item 2" }, { name: "Item 3" }]
const keyedItems = useKeyedArray(items, {
getKey: (item, index) => `item-${index}`,
})
return (
<ul>
{keyedItems.map((item) => (
<li key={item._key}>{item.name}</li>
))}
</ul>
)
}
API Reference
Parameters
Name | Type | Description | Default Value | Optional |
---|---|---|---|---|
items | Array<T> | The array of items to manage. | - | No |
options.getKey | (item: T, index: number) => string | Function to generate a unique key for each item. | (item, index) => String(index) | Yes |
Return Values
Name | Type | Description |
---|---|---|
keyedItems | Array<T & { _key: string }> | The array of items with unique keys added. |