useFieldArray: UseFieldArrayProps
Custom hook for working with Field Arrays (dynamic form). The motivation is to provide better user experience and performance. You can watch this short video to visualize the performance enhancement.
Props
Name | Type | Required | Description |
---|---|---|---|
name | string | ✓ | Name of the field array. |
control | Object | control object provided by useForm . It's optional if you are using FormContext. | |
shouldUnregister | boolean | Whether Field Array will be unregistered after unmount. |
Examples
Return
Name | Type | Description |
---|---|---|
fields | object & { id: string } | This object contains the defaultValue and key for your component. |
append |
| Append input/inputs to the end of your fields and focus. The input value will be registered during this action. Important: append data is required and not partial. |
prepend | (obj: object | object[], focusOptions) => void | Prepend input/inputs to the start of your fields and focus. The input value will be registered during this action. Important: prepend data is required and not partial. |
insert | (index: number, value: object | object[], focusOptions) => void | Insert input/inputs at particular position and focus. Important: insert data is required and not partial. |
swap |
| Swap input/inputs position. |
move |
| Move input/inputs to another position. |
update |
| Update input/inputs at particular position. Important: update data is required and not partial. |
replace |
| Replace the entire field array values. |
remove |
| Remove input/inputs at particular position, or remove all when no index provided. |
Rules
useFieldArray
automatically generates a unique identifier namedid
which is used forkey
prop. For more information why this is required: https://reactjs.org/docs/lists-and-keys.html#keysThe
field.id
(and notindex
) must be added as the component key to prevent re-renders breaking the fields:You can not call actions one after another. Actions need to be triggered per render.
Each
useFieldArray
is unique and has its own state update, which means you should not have multiple useFieldArray with the samename
.Each input name needs to be unique, if you need to build checkbox or radio with the same name then use it with
useController
orcontroller
.Does not support flat field array.
When you append, prepend, insert and update the field array, the obj can't be empty object
rather need to supply all your input's defaultValues.
TypeScript
when register input
name
, you will have to cast them asconst
we do not support circular reference. Refer to this this Github issue for more detail.
for nested field array, you will have to cast the field array by its name.
Examples
Tips
Custom Register
You can also register
inputs at Controller
without the actual input. This makes useFieldArray
quick and flexible to use with complex data structure or the actual data is not stored inside an input.
Controlled Field Array
There will be cases where you want to control the entire field array, which means each onChange reflects on the fields
object.
Thank you for your support
If you find React Hook Form to be useful in your project, please consider to star and support it.