useFieldArray Composable

The useFieldArray composable is a utility function that helps you manage arrays of fields in your form. This composable is useful when you have a dynamic list of fields that can be added or removed by the user.

📦 Usage

To use the useFieldArray composable, you need to pass the formik object as an argument.

Note: formik parameter

The formik parameter is the object returned by the useFormik composable.

📚 API

The composable returns the following array-manipulation helpers. All of them read and write through the formik instance immutably (the array is cloned before mutation), and out-of-bounds indices log a warning and no-op.

push (field: string, value: unknown, index?: number)

Appends value to the array. If index is given, inserts at that index instead.

pop (field: string, index?: number)

Removes the last item, or the item at index when provided.

insert (field: string, index: number, value: unknown)

Inserts value at index, shifting later items right.

remove (field: string, index: number)

Removes the item at index, shifting later items left.

move (field: string, from: number, to: number)

Moves an item from one index to another.

swap (field: string, indexA: number, indexB: number)

Swaps the items at two indices.

replace (field: string, index: number, value: unknown)

Replaces the item at index with a new value.

Note: Out of Bounds

If an index is out of bounds, the function logs a warning to the console and makes no change.