Skip to content

useFieldArray

React hooks for Field Array

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

NameTypeRequiredDescription
namestring

Name of the field array.

controlObjectcontrol object provided by useForm. It's optional if you are using FormContext.
shouldUnregisterboolean

Whether Field Array will be unregistered after unmount.

Examples

Return

NameTypeDescription
fieldsobject & { id: string }This object contains the defaultValue and key for your component.
append(obj: object | object[], focusOptions) => void

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(from: number, to: number) => voidSwap input/inputs position.
move(from: number, to: number) => voidMove input/inputs to another position.
update(index?: number, obj: object) => void

Update input/inputs at particular position.

Important: update data is required and not partial.

replace(obj: object[]) => voidReplace the entire field array values.
remove(index?: number | number[]) => voidRemove input/inputs at particular position, or remove all when no index provided.

Rules

  • useFieldArray automatically generates a unique identifier named id which is used for key prop. For more information why this is required: https://reactjs.org/docs/lists-and-keys.html#keys

    The field.id (and not index) 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 same name.

  • Each input name needs to be unique, if you need to build checkbox or radio with the same name then use it with useController or controller.

  • 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 as const

  • 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.

Edit