Skip to content
On this page

Array Field

An ArrayField is a wrapper used to handle arrays of form items. An example ArrayField usage is:

jsx
<FieldArray name={"numbers"} initialValue={[1]}>
  {({ add, value }) => (
    <>
      {value.map((num) => (
        <p key={num}>{num}</p>
      ))}
      <button onClick={() => add(1)}>Set value</button>
    </>
  )}
</FieldArray>

Array Field Props

The ArrayField component takes the following props:

PropertyTypeDescription
namestringThe name of the array field within the form.
initialValueT[]The initial value of the field array.
resetWithValueT[]The value to which a field array should be reset upon calling the reset() method.
preserveValuebooleanPreserve the field's values when unmount.
listenTostring[]A list of form field names to listen to. When a listened field updates it's value, it will trigger the relevant onChangeValidation change detection. Useful when making one field depend on the validation of another.
children(props: FieldArrayInstance<T>) => JSX.ElementPassed FieldArrayInstance, expected to return a JSX element.
onChangeValidate() => Promise<boolean> or ZodTypeThe validation logic for when the user has changed the field value. Either a Zod type or Promise. If resolved, no error is passed. If rejected, rejection string is set as an error.
onMountValidate() => Promise<boolean> or ZodTypeThe validation logic for when the component is mounted. Either a Zod type or Promise. If resolved, no error is passed. If rejected, rejection string is set as an error.
onSubmitValidate() => Promise<boolean> or ZodTypeThe validation logic for when the user has submitted the form. Either a Zod type or Promise. If resolved, no error is passed. If rejected, rejection string is set as an error.
onChangeHint() => Promise<boolean> or ZodTypeThe soft validation logic for when the user has changed the field value. Either a Zod type or Promise. If resolved, no hint is passed. If rejected, rejection string is set as a hint.
onMountHint() => Promise<boolean> or ZodTypeThe soft validation logic for when the component is mounted. Either a Zod type or Promise. If resolved, no hint is passed. If rejected, rejection string is set as a hint.
onSubmitHint() => Promise<boolean> or ZodTypeThe soft validation logic for when the user has submitted the form. Either a Zod type or Promise. If resolved, no hint is passed. If rejected, rejection string is set as a hint.
onSubmitTransform(value: T) => any or ZodTypeThe transform function for when the user has submitted the form. Either a Zod transform or Function. The form will receive the transformed value instead if set.
memoChildany[]An array of items passed to the inner useMemo which helps prevent re-renders on the field array.

Interface FieldArrayInstance

PropertyTypeDescription
valueTT is the type of the Field that's passed to the <Field<T>> component.
setValue(val: T) => voidA function useful to change the value of a field
setValues(val: T[]) => voidA function useful to change the value of the form array.
errorsstring[]The list of errors currently applied to the field.
setErrors(errors: string[]) => voidA way to set the errors present on the field.
hintsstring[]The list of hints currently applied to the field.
setHints(hints: string[]) => voidA way to set the hints present on the field.
isValidbooleanA helper property to check if errors is an empty array.
isValidatingbooleanA helper property to check if the field is running a validation.
isTouchedbooleanA boolean to say if the field has been focused and blurred, regardless of user input.
setIsTouched(val: boolean) => void
isDirtybooleanA boolean to say if the field has had any kind of user input.
setIsDirty(val: boolean) => void
propsArrayFieldPropsThe properties originally passed to a field from the component.
add(val: T) => voidA helper utility to add an item to the form array.
remove(index: number) => voidA helper utility to remove an item via an index from the form array.
insert(index: number, val: T) => voidA helper utility to insert an item at the index to the form array.
move(from: number, to: number) => voidA helper utility to move an item from one index to another in the form array.
replace(index: number, val: T) => voidA helper utility to replace an item at an index on the form array.
swap(indexA: number, indexB: number) => voidA helper utility to swap two items on the form array.
validate(rule: 'onChangeValidate' | 'onSubmitValidate' | 'onMountValidate' | 'onBlurValidate') => voidA method of running manual change detection on a field arrary.