fn

useFieldArray

v0.0.14testeddemo

Manage a dynamic array field within a useForm. Exposes a reactive fields list with stable keys (preserved across reorders, so v-for :key keeps DOM/state intact) plus immutable push/prepend/insert/ remove/move/swap/replace/update operations that also re-key the matching errors and touched state.

Example

ts
const form = useForm({ initialValues: { users: [{ name: '' }] } });
const { fields, push, remove } = useFieldArray('users');
// <div v-for="(field, i) in fields" :key="field.key">
//   <input v-model="field.value.name"> <button

Demo

Loading demo…

Signature

ts
export function useFieldArray<T = unknown>(
  path: MaybeRefOrGetter<string>,
  options: UseFieldArrayOptions ={ ... }

Type Parameters

T= unknown

Parameters

ParameterTypeDescription
pathMaybeRefOrGetter<string>The dotted path of the array field
options?UseFieldArrayOptionsOptionally an explicit form

Returns

UseFieldArrayReturn<T>The reactive entries and mutation helpers
PropertyTypeDescription
fieldsReadonly<Ref<Array<FieldArrayEntry<T>>>>The reactive list of entries with stable keys.
push(value: T) => voidAppend an item.
prepend(value: T) => voidPrepend an item.
insert(index: number, value: T) => voidInsert an item at an index.
remove(index: number) => voidRemove the item at an index.
move(from: number, to: number) => voidMove an item from one index to another.
swap(indexA: number, indexB: number) => voidSwap two items.
replace(values: T[]) => voidReplace the whole array.
update(index: number, value: T) => voidReplace a single item.