Skip to content

Scenario - submit the record #4

@skorphil

Description

@skorphil

What is the most appropriate way of submitting the data?

react-hook-form provides internal mechanisms for that:

They use either action or onSubmit.
onSubmit fires before action is performed – form-submission-algorithm

  1. Validation is passed useForm({resolver: yupResolver(schema)})
  2. onSubmit is fired
  3. action is performed
// Use action prop to make post submission with formData
      <Form
        action="/api"
        control={control}
        onSuccess={() => {
          alert("Success")
        }}
        onError={() => {
          alert("error")
        }}
      >
        {" "}
        <input {...register("name")} />
        {isSubmitSuccessful && <p>Form submit successful.</p>}
        {errors?.root?.server && <p>Form submit failed.</p>} <button>submit</button>
      </Form>
      // Manual form submission
      <Form
        onSubmit={async ({ formData, data, formDataJson, event }) => {
          await fetch("api", {
            method: "post",

            body: formData,
          })
        }}
      >

Metadata

Metadata

Assignees

Labels

enhancementNew feature or request

Projects

Status
Done

Relationships

None yet

Development

No branches or pull requests

Issue actions