我正在使用useState設置一個狀態變量,無論是否點擊了recaptcha,但是當我使用setState函數時,我的驗證碼變量在提交時沒有傳遞,不明白為什么,如果我刪除setState,驗證碼變量就會傳遞給我的onSubmit。如果我刪除setSubmitButton(false),一切都很好,不太確定為什么。當我運行 setSubmittButton(false) 驗證碼在我的提交函數中受到歡迎時,當我沒有它時,我在提交函數中得到了正確的驗證碼值。import React, { useState } from "react"import { useForm } from "react-hook-form"import ReCAPTCHA from "react-google-recaptcha"const ContactForm = () => { const { register, handleSubmit, watch, errors } = useForm() const isBrowser = typeof window !== `undefined` let location if (isBrowser) { location = window.location.hostname } let fetchUrl if (location === "localhost") { fetchUrl = `http://localhost:8888/.netlify/functions/contact` } else if (location === "fsdf.gtsb.io") { fetchUrl = `https://fdsfd/.netlify/functions/contact` } else { fetchUrl = "/.netlify/functions/contact" } console.log(fetchUrl) const onSubmit = async data => { setLoading(true) console.log(captcha, "captcha value final") const response = await fetch(fetchUrl, { method: "POST", body: JSON.stringify({ data, captcha: captcha }), }) .then(response => response.json()) .then(data => { console.log(data) if (data.captcha === false) { setCaptchaFailed(true) } }) .catch(error => { console.error("Error:", error) }) } const [submitButton, setSubmitButton] = useState(true) const [loading, setLoading] = useState(false) const [captchaFailed, setCaptchaFailed] = useState(false) let captcha function onChange(value) { setSubmitButton(false) // IF I REMOVE THIS LINE EVERYTHING WORKS FINE ****** console.log("Captcha value:", value) captcha = value } function error(value) { alert(value) }
useState with react 給我問題 onChange 處理程序
慕標5832272
2022-09-02 17:05:03