React contact form to email
A single React component is enough. It posts the form's fields from the browser to FormRoute and shows whether the message was sent. The example uses Vite's environment variables; any React setup works the same way.
You need a FormRoute account with a verified domain and a form whose fields are name, email and message, or change the names below to match yours. Getting started covers the setup.
1. Add your site key
Vite exposes variables that start with VITE_ to the browser. The site key is public by design.
VITE_FORMROUTE_SITE_KEY=fr_your_site_key2. Create the component
The name attributes must match the fields you set up in FormRoute. The clock for t starts when the component mounts.
import { useEffect, useRef, useState, type FormEvent } from "react";
const ENDPOINT = "https://form-route.com/api/v1/send";
const SITE_KEY = import.meta.env.VITE_FORMROUTE_SITE_KEY;
export function ContactForm() {
const shownAt = useRef(0);
const [status, setStatus] = useState<"idle" | "sending" | "sent" | "error">("idle");
// Start the clock when the form is on screen.
useEffect(() => {
shownAt.current = Date.now();
}, []);
async function onSubmit(event: FormEvent<HTMLFormElement>) {
event.preventDefault();
const form = event.currentTarget;
const data = new FormData(form);
setStatus("sending");
try {
const response = await fetch(ENDPOINT, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({
siteKey: SITE_KEY,
t: Date.now() - shownAt.current,
hp: data.get("hp") ?? "",
name: data.get("name"),
email: data.get("email"),
message: data.get("message"),
}),
});
if (response.ok) {
form.reset();
setStatus("sent");
} else {
setStatus("error");
}
} catch {
setStatus("error");
}
}
return (
<form onSubmit={onSubmit}>
<label>Name <input name="name" required maxLength={120} /></label>
<label>Email <input name="email" type="email" required maxLength={254} /></label>
<label>Message <textarea name="message" required maxLength={5000} /></label>
{/* Honeypot: hidden from people, must stay empty */}
<div aria-hidden="true" style={{ position: "absolute", left: -10000 }}>
<label>Leave this empty <input name="hp" tabIndex={-1} autoComplete="off" /></label>
</div>
<button type="submit" disabled={status === "sending"}>Send</button>
<p role="status">
{status === "sent" && "Thanks, your message was sent."}
{status === "error" && "Sorry, that didn't go through. Please try again."}
</p>
</form>
);
}3. List your website in FormRoute
In your FormRoute account, open your form and add every address the form is served from to Allowed websites, for example https://www.example.com and, while you develop, http://localhost:5173. Each entry is a scheme and host, with a port only if your site uses one.
A browser submission from an address that isn't listed is refused, and your account shows it as Rejected: website not allowed. For local development, use the exact address your dev server prints.
4. Send a test
Open the page, fill in the form and send it. The submission appears on your FormRoute home page within seconds, as Sent or with the reason it was refused, and the email arrives from your own domain.
Fill the form in at a human pace: a submission sent within 1.2 seconds of the form appearing is treated as automated.
Good to know
- Using Create React App or another bundler? Replace
import.meta.env.VITE_FORMROUTE_SITE_KEYwith that tool's way of reading a public variable, or paste the key directly. - Deploying? Set the same environment variable in your host's settings (for example your Vercel or Netlify project):
.envfiles on your machine are not deployed. Or put the key in the component as a constant; it is public by design.
More guides
HTML · Next.js · Vue · Astro · WordPress · Webflow · Framer · Hugo, Jekyll and Eleventy