useState in Forms

Pascual Vila
Frontend Instructor.
useState is a hook frequently used in controlled forms to manage the state of inputs. Every time the user changes the value of a field, the state is updated, which in turn causes the component to re-render.
Example with useState:
{`import React, { useState } from "react";
function Form() {
const [email, setEmail] = useState("");
const handleEmailChange = (event) => {
setEmail(event.target.value);
};
const handleSubmit = (event) => {
event.preventDefault();
alert("Email entered: " + email);
};
return (
<form onSubmit={handleSubmit}>
<input
type="email"
value={email}
onChange={handleEmailChange}
placeholder="Enter your email"
/>
<button type="submit">Submit</button>
</form>
);
}
export default Form;`}