<Item = any>(name: string, formId?: string) => [itemDefaults: Item[], helpers: FieldArrayHelpers<Item>]
A hook that makes it easy to work with dynamic arrays of values. It returns a tuple that contains the default values for each item and the helpers for manipulating the array.
Note
Example
Parameters
string
The name of the "field". If you're using array syntax, then this is the base name of the field (not include array index specifier).
Example: If you're items named todos[0]
, todos[1]
, etc, then the name would be todos
.
stringOptional
Allows you to use the hook outside a form.
FieldArrayHelpers
Many of these helpers parallel native array helpers, but please note that, unlike the native array methods, none of these helpers return anything.
(item: Item) => void
Adds a new item to the end of the array.
(indexA: number, indexB: number) => void
Swaps the items at the given indices.
(from: number, to: number) => void
Moves the item at the from
index to the to
index.
(index: number, item: Item) => void
Inserts an item at the given index.
() => void
Removes the item at the start of the array.
() => void
Removes the item at the end of the array.
(index: number) => void
Removes the item at the given index.
(index: number, item: Item) => void
Replaces the item at the given index.