A Gatsby contact form that works on any host
Save one component and every message from your Gatsby site reaches your inbox, with the error shown under the field when an address is mistyped. It works on any host, Netlify or not. Messages are stored in the EU and spam is filtered out.
14 days free, no card. Paste the address where the code says YOUR_FORM_ID.
import * as React from "react";
type Problem = { field?: string; message: string };
export function ContactForm() {
const [sending, setSending] = React.useState(false);
const [sent, setSent] = React.useState(false);
const [problems, setProblems] = React.useState<Problem[]>([]);
async function send(event: React.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
Save the component
Save the code above as src/components/ContactForm.tsx, with your own address in the action. In a JavaScript project, name it ContactForm.jsx and drop the two type lines.
-
3
Put it on a page
In src/pages/contact.tsx, import { ContactForm } from "../components/ContactForm" and render <ContactForm /> where the form belongs.
-
4
Build and test
Run gatsby build and gatsby serve, fill in the form and check your inbox. Localhost is always allowed, so you can test before you deploy.
The page that shows it
A page in src/pages becomes /contact/. The Head export sets its title.
import * as React from "react";
import type { HeadFC } from "gatsby";
import { ContactForm } from "../components/ContactForm";
export default function ContactPage() {
return (
<main>
<h1>Contact</h1>
<ContactForm />
</main>
);
}
export const Head: HeadFC = () => <title>Contact</title>;
Stop bots that skip the hidden field
Turn on the bot check in the form's settings and add the script to every page's HTML at build time, in gatsby-ssr.tsx at the root of the site. It solves its puzzle in the background and adds its answer as the form is sent, so React's hydration never sees a field it did not render. Visitors see nothing, and it sets no cookies.
import * as React from "react";
import type { GatsbySSR } from "gatsby";
export const onRenderBody: GatsbySSR["onRenderBody"] = ({ setHeadComponents }) => {
setHeadComponents([
<script key="formhaven-bot-check" src="https://formhaven.net/bot-check.js" async />,
]);
};
Questions about this setup
- Why not Netlify Forms?
- Netlify Forms only works while the site is hosted on Netlify. FormHaven works on any host, stores messages in the EU under a data processing agreement, and can send each message to your client too.
- Does it work without JavaScript?
- Yes. 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.
- Which hosts does this work on?
- Any host that serves the public folder gatsby build writes: Netlify, Cloudflare Pages, GitHub Pages, Vercel or your own server. The form posts to FormHaven, so nothing runs on the host when a visitor presses Send.
- 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 servers in the EU, for 1 to 365 days as you choose per form, or not at all: then each message is emailed and its answers deleted. 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.