Many real-world forms require the user to enter a variable number of items — a list of phone numbers, a set of product variants, a collection of addresses. Managing a dynamic list withDocumentation Index
Fetch the complete documentation index at: https://mintlify.com/rijvi-mahmud/shaddy/llms.txt
Use this file to discover all available pages before exploring further.
react-hook-form’s useFieldArray requires wiring up the hook, passing control, and tracking the array state yourself. FieldArray wraps all of that into a single declarative component.
How It Works
FieldArray uses the function-as-children (render props) pattern. It calls useFieldArray internally with the correct control from useFormContext, then passes the full UseFieldArrayReturn object to your render function. You get fields, append, remove, prepend, update, and every other method from useFieldArray without writing any hook calls yourself.
Installation
Props
The dot-notation path to the array field in your schema. Must match an array key in your Zod schema (e.g.,
"items", "phoneNumbers"). The generic type T is inferred from the surrounding ShaddyForm.A render function that receives the full
useFieldArray return value. Use the destructured fields, append, and remove (among others) to build your dynamic list UI.Usage
Thechildren function receives a UseFieldArrayReturn object. The most commonly used properties are:
| Property | Type | Description |
|---|---|---|
fields | FieldArrayWithId[] | The current list of items. Each has a stable id to use as the React key. |
append | (value) => void | Adds a new item to the end of the list. |
prepend | (value) => void | Adds a new item to the beginning of the list. |
remove | (index) => void | Removes the item at the given index. |
update | (index, value) => void | Replaces the item at the given index. |
move | (from, to) => void | Moves an item from one position to another. |
Example: Dynamic Contact List
The example below builds a form that lets the user add and remove contact entries, each containing a name and an email address.Always use
field.id (not index) as the React key for array items. react-hook-form assigns a stable, unique id to each entry that does not change when items are reordered or removed. Using index as a key causes subtle state bugs when the list changes.Nesting FieldArrays
FieldArray can be nested to support multi-level dynamic structures. Ensure that each name prop reflects the correct dot-notation path to the nested array within the schema.
Because TypeScript’s ArrayPath<T> and Path<T> do not accept template literal strings, you must cast dynamic index paths using as when building nested field names at runtime.