简体   繁体   English

react-hook-form 'on Submit' function 代码未执行

[英]react-hook-form 'on Submit' function code is not executing

If a form submits successfully, I want the onSubmit function to be called.如果表单提交成功,我希望调用onSubmit function。

The validation is working correctly, but the onSubmit function is not being called and there are no errors showing so I can not figure out the issue.验证工作正常,但未调用onSubmit function 并且没有显示错误,因此我无法解决问题。

This is the onSubmit function causing the problem:这是导致问题的onSubmit function:

 const onSubmit: SubmitHandler<Props> = (data) => console.log("testing");

and I am calling it with this line:我用这条线来称呼它:

<form onSubmit={handleSubmit(onSubmit)} ref={form}>

I am using the react-hook-form library and yup to validate forms.我正在使用 react-hook-form 库和是的来验证 forms。 I am also using react, and typescript.我也在使用 react 和 typescript。

This is the rest of the code:这是代码的rest:

import React, { useRef, useState } from "react";
import { useForm } from "react-hook-form";
import { yupResolver } from "@hookform/resolvers/yup";
import { string, number, object, InferType } from "yup";


type Props = InferType<typeof schema>;

const schema = object({
  firstName: string().required("First name is required"),
});

function FormEmail() {
  const form = useRef(null);

  const {
    register,
    handleSubmit,
    formState: { errors },
  } = useForm<Props>({
    resolver: yupResolver(schema),
  });

 const onSubmit: SubmitHandler<Props> = (data) => console.log("testing");

return (
    <div>
      <form onSubmit={handleSubmit(onSubmit)} ref={form}>

            <h3>First Name</h3>
            <input
              id="firstName"
              type="text"
              {...register("firstName")}
            />
            <span className="error">{errors?.firstName?.message}</span>

            <button className="" type="submit">
              Submit
            </button>
      </form>
    </div>
  );
}

export default FormEmail;

[docs for handleSubmit] https://react-hook-form.com/api/useform/handlesubmit/#main [handleSubmit 文档] https://react-hook-form.com/api/useform/handlesubmit/#main

Thanks for the help:)谢谢您的帮助:)

just use handleSubmit in onSubmit and you have some problem in the code... so this code will be working with you只需在onSubmit中使用handleSubmit ,您的代码就有问题......所以这段代码将与您一起使用

import React, { useRef, useState } from "react";
import { SubmitHandler, useForm } from "react-hook-form";
import { yupResolver } from "@hookform/resolvers/yup";
import { string, number, object, InferType } from "yup";

function onSubmit(values: Props) {}

const schema = object({
  firstName: string().required("First name is required")
});

type Props = InferType<typeof schema>;

function FormEmail() {
  const form = useRef(null);

  const {
    register,
    handleSubmit,
    formState: { errors }
  } = useForm<Props>({
    resolver: yupResolver(schema)
  });

  const onSubmit: SubmitHandler<Props> = (data) => console.log("testing");

  return (
    <div>
      <form onSubmit={handleSubmit(onSubmit)} ref={form}>
        <h3>First Name</h3>
        <input id="firstName" type="text" {...register("firstName")} />
        <span className="error">{errors?.firstName?.message}</span>

        <button className="" type="submit">
          Submit
        </button>
      </form>
    </div>
  );
}

export default FormEmail;

Your code is good, but you the sendEmail function is registered to form submit instead of the testing one.您的代码很好,但是您将sendEmail function 注册为表单提交而不是测试表单。

It should be just like you wrote it in the smaller snippet (this works for me):它应该就像您在较小的片段中编写的一样(这对我有用):

<form onSubmit={handleSubmit(onSubmit)} ref={form}>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM