3

当表单验证失败时,我需要重置验证码,否则我似乎无法再次提交表单。但是,在调用重置时出现错误“无法读取 null 的属性‘样式’”

import ReCAPTCHA from "react-google-recaptcha";

const MyComponent = () => {
  const recaptchaRef = React.useRef();
  const onSubmit = async (data) => {
     const token = await recaptchaRef.current.executeAsync();
    try {
      const response = await axios.post(
        `${process.env.NEXT_PUBLIC_API_ENDPOINT}/tour`,
        {
          firstName: data.firstName,
          lastName: data.lastName,
          token: token,
        }
      );
      if (response.status === 201) {
        router.push("../thank-you");
      }
    } catch (err) {
      recaptchaRef.current.reset();
      setError(err.response.data);
    }
  }
}
4

0 回答 0