以下是我目前的程式碼,它輸出了我輸入的名稱,但我想建立一個重置按鈕,將狀態清空為一個空狀態
import { useState } from "react"; function resetName() { this.firstName.value = ""; this.lastName.value = ""; } export default function Test() { const [firstName, setFirstName] = useState(""); const [lastName, setLastName] = useState(""); const fullName = `${firstName} ${lastName}`; return ( <div> 名字: <input value={firstName} onChange={(e) => setFirstName(e.target.value)} />{" "} <br /> 姓氏:{" "} <input value={lastName} onChange={(e) => setLastName(e.target.value)} />{" "} <br /> {fullName} <br /> <button value={resetName}>重置!</button> </div> ); }
呼叫
setFirstName
和setLastName
來重置輸入值。並設定
<button>
的onClick
屬性: