Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

109
Visualizações
React Hook Form v7, the POST request is sent every time I type on a field

I have a form and I want to update the values there (API request: PUT "https://www.test.com/account/update") . I'm using React Hook Form, like following:

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

const {
    control,
    register,
    handleSubmit,
    formState: { errors },
    getValues,
    setValue,
  } = useForm({
    mode: "onSubmit",
  });

const updateData = async () => {
    const data = {
      firstName: getValues("firstName"),
      lastName: getValues("lastName"),
    };

    axios.put("https://www.test.com/account/update", data, { headers: header })
   .then((response) => {
      if (response.status == 201) {
        setFirstName(response.data.firstName);
        setLastName(response.data.lastName);
        return response.data;
      } else {
        console.log("error");
      }
    });
  };

return (
    <React.Fragment>
      <IonPage>
        <IonContent>
          <IonGrid>
                <form
                  onClick={handleSubmit(updateData)}
                >
                  <IonItem>
                    <IonLabel>Firstname</IonLabel>
                    <IonInput
                      required
                      value={firstName}
                      {...register("firstName")}
                    ></IonInput>
                  </IonItem>
                  <IonItem>
                    <IonLabel>Lastname</IonLabel>
                    <IonInput
                      required
                      value={lastName}
                      {...register("lastName")}
                    ></IonInput>
                  </IonItem>
                 
                  <IonButton
                    type="submit"
                  >
                    Save
                  </IonButton>
                </form>
          </IonGrid>
        </IonContent>
      </IonPage>
    </React.Fragment>
  );
};

The problem is that the form gets submitted every time I type on a field. I want it to be submitted only when I click on submit button. I read the docs about useForm() here: https://react-hook-form.com/api/useform but I didn't found a solution. How can I change that?

Thank you in advance!

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Change onClick to onSubmit. That way form submit is triggered when button with type="submit" is clicked

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

const {
    control,
    register,
    handleSubmit,
    formState: { errors },
    getValues,
    setValue,
  } = useForm({
    mode: "onSubmit",
  });

const updateData = async () => {
    const data = {
      firstName: getValues("firstName"),
      lastName: getValues("lastName"),
    };

    axios.put("https://www.test.com/account/update", data, { headers: header })
   .then((response) => {
      if (response.status == 201) {
        setFirstName(response.data.firstName);
        setLastName(response.data.lastName);
        return response.data;
      } else {
        console.log("error");
      }
    });
  };

return (
    <React.Fragment>
      <IonPage>
        <IonContent>
          <IonGrid>
                <form
                  {/* onClick={handleSubmit(updateData)} */}
                  onSubmit={handleSubmit(updateData)}
                >
                  <IonItem>
                    <IonLabel>Firstname</IonLabel>
                    <IonInput
                      required
                      value={firstName}
                      {...register("firstName")}
                    ></IonInput>
                  </IonItem>
                  <IonItem>
                    <IonLabel>Lastname</IonLabel>
                    <IonInput
                      required
                      value={lastName}
                      {...register("lastName")}
                    ></IonInput>
                  </IonItem>
                 
                  <IonButton
                    type="submit"
                  >
                    Save
                  </IonButton>
                </form>
          </IonGrid>
        </IonContent>
      </IonPage>
    </React.Fragment>
  );
};
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda