Handling Form Submissions without Actions
As you learned in Chapter 4, Working with Events and State, when not using actions, you can handle form submissions by listening to the submit
event via the onSubmit
prop on the <form>
element.
Consider the following example code snippet:
function App() {
function handleSubmit(event) {
event.preventDefault();
console.log('Submitted!');
}
return (
<form onSubmit={handleSubmit}>
<p>
<label htmlFor="email">Email</label>
<input type="email" id="email" />
</p>
<p>
<label htmlFor="password">Password</label>
<input type="password" id="password" />
</p>
<p className="actions">
<button>Login</button>
</p>
</form>
);
}
You find the full working example on GitHub...