我正在嘗試設(shè)置一個(gè)支付網(wǎng)關(guān),但我首先想將所有賬單明細(xì)存儲(chǔ)在狀態(tài)變量中,然后傳遞給網(wǎng)關(guān),但是在輸入字段中,每當(dāng)我輸入電子郵件地址時(shí),只要我還在,它就會(huì)輸出它在輸入字段中,但是當(dāng)我選擇 first_name 字段時(shí),它的狀態(tài)似乎沒(méi)有保存并且默認(rèn)返回未定義,我覺(jué)得我在這里遺漏了什么?當(dāng)前狀態(tài):let [billingInfo, setBillingInfo] = useState({ email: "", first_name: "", last_name: "", company_name: "", address: "", extra_address: "", city: "", country: "", state: "", zip_code: "", });我如何輸出到控制臺(tái):const testM = (e) => { e.preventDefault(); console.log(billingInfo.email); console.log(billingInfo.first_name); console.log(billingInfo.last_name); }手柄輸入法:let handleInputChange = (e) => { setBillingInfo({ [e.target.name]: e.target.value }); };輸入字段示例:<form className="mt-4 "> <h4 className="mt-4">Contact Information</h4> <div className=" mb-3" style={{ height: "3rem" }}> <input type="tel" name="email" value={state.email} className="rounded" style={{ width: "100%", height: "100%" }} placeholder=" Email Address.." onChange={handleInputChange} /> </div> <h4 className="mt-4">Shipping Information</h4> <div className="row" style={{ height: "3.5rem" }}> <div className="mb-3 col-6"> <input type="tel" name="first_name" value={state.first_name} className="rounded" style={{ width: "100%", height: "100%" }} placeholder=" First Name.." onChange={handleInputChange} /> </div>
每當(dāng)切換輸入字段時(shí),狀態(tài)默認(rèn)為未定義
搖曳的薔薇
2022-12-08 15:51:14