A React contact form that emails you every message
Drop one component into a Vite app, or any React project, and every message is spam filtered, stored in Germany and emailed to you. Your bundle carries only a public form address, and the browser posts straight to us.
14 days free, no card. Paste the address where the code says YOUR_FORM_ID.
import { useState, type FormEvent } from "react";
type Problem = { field?: string; message: string };
export function ContactForm() {
const [sending, setSending] = useState(false);
const [sent, setSent] = useState(false);
const [problems, setProblems] = useState<Problem[]>([]);
async function send(event: FormEvent<HTMLFormElement>) {
event.preventDefault();
const form = event.currentTarget;
setSending(true);
const response = await fetch(form.action, {
method: "POST",
headers: { Accept: "application/json" },
body: new FormData(form),
});
const result = await response.json();
setSending(false);
setSent(result.ok);
setProblems(result.ok ? [] : result.errors);
if (result.ok) form.reset();
}
const problem = (field: string) => problems.find((p) => p.field === field)?.message;
return (
<form action="https://formhaven.net/f/YOUR_FORM_ID" method="POST" onSubmit={send}>
<label>Your email <input type="email" name="email" required /></label>
{problem("email") && <p>{problem("email")}</p>}
<label>Message <textarea name="message" required /></label>
<input type="text" name="_gotcha" style={{ display: "none" }} tabIndex={-1} autoComplete="off" />
<button type="submit" disabled={sending}>{sending ? "Sending..." : "Send"}</button>
<p role="status">{sent ? "Thanks, your message is on its way." : problems.find((p) => !p.field)?.message}</p>
</form>
);
}
Set it up
-
1
Create a form in FormHaven
Sign up, press New form and copy its address. It looks like https://formhaven.net/f/YOUR_FORM_ID.
-
2
Add the component
Save the code above as src/ContactForm.tsx, paste your address into the action, and render <ContactForm /> where the form belongs.
-
3
Read the answer
With Accept: application/json the reply is {"ok": true}, or {"ok": false, "errors": [...]} where each error has a code, a message and, when one input caused it, the field name.
-
4
Send a test
Run npm run dev, fill in the form and check your inbox. Localhost is always allowed, so you can test before you deploy.
With React 19 actions
React 19 lets a form take a function as its action and calls it with the form data. It is shorter, with two costs. React clears the fields after every send, errors included. And the form has no address of its own, so the bot check cannot find it and the form cannot post without JavaScript. Leave the bot check off.
import { useActionState } from "react";
async function send(_previous: unknown, data: FormData) {
const response = await fetch("https://formhaven.net/f/YOUR_FORM_ID", {
method: "POST",
headers: { Accept: "application/json" },
body: data,
});
return response.json();
}
export function ContactForm() {
const [result, action, pending] = useActionState(send, null);
return (
<form action={action}>
<input type="email" name="email" required />
<textarea name="message" required />
<input type="text" name="_gotcha" style={{ display: "none" }} tabIndex={-1} autoComplete="off" />
<button type="submit" disabled={pending}>Send</button>
<p role="status">{result?.ok ? "Thanks, your message is on its way." : result?.errors?.[0]?.message}</p>
</form>
);
}
A test form and a live form
Keep the address in a Vite environment variable and point development at a second form, so your tests never land in the live inbox. The address is public either way: it can receive messages, never read them.
VITE_FORM_URL=https://formhaven.net/f/YOUR_TEST_FORM_ID
# in ContactForm.tsx:
# <form action={import.meta.env.VITE_FORM_URL} method="POST" onSubmit={send}>
Stop bots that skip the hidden field
Turn on the bot check in the form's settings and add this line once to index.html. It finds forms React draws after the page loads. Visitors see nothing, and it sets no cookies.
<script src="https://formhaven.net/bot-check.js" async></script>
Questions about this setup
- Why not EmailJS or an email API?
- An email API needs a key that must stay off the page, so you end up running a server anyway. FormHaven gives you one public address, with spam filtering and a place to read old messages.
- Can an agent set this up for me?
- Yes. Ask Claude Code or Cursor to add a FormHaven contact form. Through our MCP server it can fetch a ready React component for your form with get_form_code.
- Does it still work without JavaScript?
- Yes, in the main version. The form has a real action and method, so the browser posts it the normal way and the visitor sees our thank-you page, or yours if you set one on the form.
- Can my client get the messages instead of me?
- Yes. Add up to 5 more addresses to the form. Each one confirms by a link first, and their copy carries no FormHaven branding. You can turn your own copy off.
- Where is the data stored?
- On our server at Hetzner in Falkenstein, Germany, for 1 to 365 days as you choose per form, or not at all. A data processing agreement covers every account.
More to read
Get your form address in a minute
14 days free, no card. Then €1 per form a month, €0.50 from the 11th form.