Last active
January 17, 2022 16:00
-
-
Save KolbySisk/14ddd223609737237bf17a2f8b33ea6b to your computer and use it in GitHub Desktop.
Simple Form
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export default function SimpleForm() { | |
const handleSubmit = async (event) => { | |
event.preventDefault(); | |
// Use FormData to get the input values | |
const formData = new FormData(event.target); | |
// Optionally, convert FormData into an object | |
const dataObject = Object.fromEntries(formData); | |
// Process the data | |
await fetch("/api/form", { | |
method: "POST", | |
body: JSON.stringify(dataObject) | |
}); | |
// Clear the form | |
event.target.reset(); | |
}; | |
return ( | |
<form onSubmit={handleSubmit}> | |
<input name="email" type="email" required /> | |
<input name="password" type="password" required /> | |
<button>Submit</button> | |
</form> | |
); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment