Skip to content
On this page

Form

The Form component is the main component of this library. It is responsible for managing the state of the form and must be the parent of all Field components.

An example Form usage is:

jsx
<Form onSubmit={() => {}}>
  {({ submit }) => <button onClick={submit}>Submit</button>}
</Form>

Form Props

The Form component takes the following props:

MethodParametersExpected ReturnDescription
onSubmitRecord<string, any>, FormInstanceThe function to call when the form is submitted. The first argument is the values of a form submitted. It might look something like:
{email: "test@example.com", password: "Hunter2!", confirmpassword: "Hunter2!"}
childrenFormInstanceJSX.ElementThis is the component child function to pass, which accepts the arguments for FormInstance.
memoChildany[]An array of items passed to the inner useMemo which helps prevent re-renders on the form.
submitWhenInvalidbooleanTypically, when a form's fields are invalid, the onSubmit function will not run. Passing true to submitWhenInvalid bypasses this functionality.

Interface FormInstance

These are the properties that are passed to the <Form> component's child function, the FormContext, and the second arguments of the onSubmit function as well as all <Field> onXValidate property functions:

PropertyTypeDescription
submit() => voidThe function to run when you're ready to submit your form. This function will not do anything if there are errors on the form.
reset() => voidThe function to call when resetting the form to its default values specified by resetWithValue. It will also reset the form's error, dirty, and touched states.
errorsstring[]An array of all errors on all form fields.
errorsMapRecord<string, string[] or DeepMap<T, string[]>An object that stores errors by field name.

If you've passed a type T to Form, the TypeScript type of errorsMap is instead a deep map of T and all deeply nested fields are string[].
isValidbooleanA boolean to check if the form is valid or not.
isValidatingbooleanA boolean to check if the form fields are running a validation.
isSubmittedbooleanA boolean to check if the form has had an attempted submission or not.
setIsSubmitted(val: boolean) => voidA method to reset the isSubmitted field
isDirtybooleanA boolean to check if any of the form fields are dirty or not.
setIsDirty(val: boolean) => voidA method to reset the isDirty properties of all of the Form's Fields.
isTouchedbooleanA boolean to check if any of the form fields have been touched or not.
setIsTouched(val: boolean) => voidA method to reset the isTouched properties of all of the Form's Fields.
getFieldValue(fieldName: sting) => FieldInstanceTakes the field name and returns a FieldInstance or FieldArrayInstance representation of the named field.
valueRecord<string, any>The values of a form's fields. It might look something like:
{email: "test@example.com", password: "Hunter2!", confirmpassword: "Hunter2!"}