Aloysious' Codefellows Reading Notes
<== Previous Lesson Next Lesson ==>
<== Home 🏠
letting the browser handle the state:
export function LoginForm() {
const handleSubmit = (e: React.FormEvent) => {
e.preventDefault();
const formData = new FormData(e.target as HTMLFormElement);
api.login(formData.get('email'), formData.get('password'));
};
return (
<form onSubmit={handleSubmit}>
<div>
<label htmlFor="email">Email</label>
<input
type="email"
id="email"
name="email"
/>
</div>
<div>
<label htmlFor="password">Password</label>
<input
type="password"
id="password"
name="password"
/>
</div>
<button>Log in</button>
</form>
);
}
// Not a single Hook in sight, no setting the value, and no change listeners either.
// https://blog.logrocket.com/forms-in-react-in-2020/
FormData
built-in browser API FormData is a handy (and well supported) way to get the field values from our input fields!<span class="pln">defaultValue</span>
prop to get that done as well!<== Home 🏠