React Flexible FormDocsExamplesGitHub
DocumentationOverviewGetting StartedTypeScriptAPIuseFormFielduseSubformuseFieldArrayuseConvertutilsExamplesSimple FormChakra UI FormForm with ArraySubformCustom useConvertForm State in ReduxForm with LoaderForm based on Material UIThrottled error validation

useForm

useForm is used to create a formControl. formControl is then passed to each <Field /> or can be used directly. It has all methods to change form state (like setFieldValue) and form start itselft (like values)

import { useForm } from "react-flexible-form";

const onSubmit = useCallback(() => {}, []);

const formControl = useForm({
  initialValues: { firstName: "", lastName: "" },
  onSubmit,
});

Essentially, useForm is a collection of default hooks that implement form functionality. First, it is checked where form state should be stored. If setFieldValues, setFieldError, setFieldTouched and setFieldDirty are not passed, they are created internally. Then it calls several hooks, useValidate to validate the form, useInitialValues to set initial values, useDirty to calculate if form is dirty and useSubmitCreator which create handleSubmit function. At the end, rffFormControl is created be memoizing results of the previous steps. rffFormControl includes all functions to manipulate a form.

Parameters

    onSubmit: (arg: {
      rffFormControl: Omit<FormControl<Values>, "handleSubmit">;
      submitProps: SubmitProps;
    }) => Promise<SubmitReturn> | SubmitReturn

Parameters to customize form internals

Parameters below are optional and are used to customize how form behave.

Results

useForm returns formControl object which represents the form.

    type Values = {
      firstName: string
      lastName: string
      age: number
      preferences: {
        sendEmail: boolean
        sendSms: boolean
      }
    }

name can be firstName, lastName, age, preferences, preferences.sendEmail, preferences.sendSms or ''. value is a value to set or a function (prev: Value) => Value (like in React's setState). value is also typechecked. If name is age, value should be number or function returning number.