Skip to content

Instantly share code, notes, and snippets.

@KolbySisk
Last active January 17, 2022 16:00
Show Gist options
  • Save KolbySisk/14ddd223609737237bf17a2f8b33ea6b to your computer and use it in GitHub Desktop.
Save KolbySisk/14ddd223609737237bf17a2f8b33ea6b to your computer and use it in GitHub Desktop.
Simple Form
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