- Neat trick to handle form data using
FormData
. It converts form data into a js object.
const handleSubmit = async (event: any) => {
event.preventDefault();
const form = new FormData(event.target);
const formData = Object.fromEntries(form.entries());
await fetch('/api/contact', {
body: JSON.stringify(formData),
headers: {
'Content-Type': 'application/json',
},
method: 'POST',
});
// const result = await res.json();
};
- Learnt from https://youtu.be/DOIWQddRD5M?t=535