React
A hooks-only React 19 component that posts JSON straight from the browser, with idle, sending, sent and error states and Turnstile via the explicit render API.
Sample: samples/react (ContactForm.tsx, README). Works on React 18 too; no dependencies beyond React.
The snippet
import ContactForm from './ContactForm';
export function ContactPage() {
return (
<ContactForm
formId="au_xxxxxxx"
turnstileSiteKey="0x4AAAAAAA_PLACEHOLDER"
subject="Website enquiry"
onSent={(id) => console.log('sent', id)}
/>
);
}What the component does
- State machine:
statusisidle,sending,sent(with the submission id) orerror(with a message and, for429,retryAfter). While sending, the inputs and button are disabled andaria-busyis set. - Submit:
FormDatabecomes a plain object, plus_page(current URL), optional_subject, andcf-turnstile-responsefrom the Turnstile callback (falling back toturnstile.getResponse). Thenfetchtohttps://api.contactusiq.com/s/{formId}withContent-Type: application/jsonandAccept: application/json. - Responses:
200setssent;400shows the server'serrorstring;429shows theRetry-Afterseconds;413says the message is too long; anything else or a network failure shows a generic error. Turnstile is reset after every attempt. - Turnstile: the script is injected once with
?render=explicit; auseEffectrenders the widget into arefon mount and removes it on unmount, so it survives strict-mode double mounting and route changes. OmitturnstileSiteKeyto submit without Turnstile. - Honeypot:
_honeypotis a real text input positioned off-screen withtabIndex={-1}andautoComplete="off". - Markup: plain
label,input,textarea,buttonandp[role=alert]elements with no styles, so your CSS applies.
The core of the submit handler:
const res = await fetch(`https://api.contactusiq.com/s/${encodeURIComponent(formId)}`, {
method: 'POST',
headers: { 'Content-Type': 'application/json', Accept: 'application/json' },
body: JSON.stringify(fields),
});
if (res.status === 200) {
const data = (await res.json()) as { id?: string };
setStatus({ state: 'sent', id: data.id });
} else if (res.status === 429) {
const retryAfter = Number(res.headers.get('Retry-After')) || 60;
setStatus({ state: 'error', message: `Try again in ${retryAfter} seconds.`, retryAfter });
}Steps
- Copy
ContactForm.tsxinto your components folder. - Render it with your form id and Turnstile site key.
- Deploy on the verified domain: JSON posts are cross-origin, and the CORS preflight is answered for verified domains only.
localhostis never verified.
Troubleshooting
| Symptom | Cause and fix |
|---|---|
| CORS error, request blocked | Origin not verified on the form. Verify the domain or allow any origin for development. |
200 but nothing in the inbox | Silent origin or spam rejection. Check the domain and that the honeypot is empty. |
| Turnstile does not render | Wrong site key, domain not listed on the Turnstile widget, or CSP blocking https://challenges.cloudflare.com. |
For a server component page with a client form, see Next.js.