How can I reset the input field if I use it this way?
const [email, setEmail] = useState({ email: "", emailError: "" });
const handleEmail = (inputEmail) => {
if (/^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(inputEmail)) {
setEmail({ email: inputEmail, emailError: "" });
} else {
setEmail({ email: "", emailError: "Invalid Email" });
}
};
<Form.Control type="email" onChange={(e) => handleEmail(e.target.value)} name="email"
placeholder="Enter email" />
You can use initial state variable, this is one of the best approach to reset states in react. If you want to reset seperately then you can give initial values for email and emailError instead of giving whole initial object
const initialState = {
email: "",
emailError: ""
}
const [email, setEmail] = useState(initialState);
const reset = () => {
setEmail(initialState)
}