---
title: React
description: 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`](/samples/react/README.md) (`ContactForm.tsx`, README). Works on React 18 too; no dependencies beyond React.

## The snippet

```tsx
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**: `status` is `idle`, `sending`, `sent` (with the submission id) or `error` (with a message and, for `429`, `retryAfter`). While sending, the inputs and button are disabled and `aria-busy` is set.
- **Submit**: `FormData` becomes a plain object, plus `_page` (current URL), optional `_subject`, and `cf-turnstile-response` from the Turnstile callback (falling back to `turnstile.getResponse`). Then `fetch` to `https://api.contactusiq.com/s/{formId}` with `Content-Type: application/json` and `Accept: application/json`.
- **Responses**: `200` sets `sent`; `400` shows the server's `error` string; `429` shows the `Retry-After` seconds; `413` says 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`; a `useEffect` renders the widget into a `ref` on mount and removes it on unmount, so it survives strict-mode double mounting and route changes. Omit `turnstileSiteKey` to submit without Turnstile.
- **Honeypot**: `_honeypot` is a real text input positioned off-screen with `tabIndex={-1}` and `autoComplete="off"`.
- **Markup**: plain `label`, `input`, `textarea`, `button` and `p[role=alert]` elements with no styles, so your CSS applies.

The core of the submit handler:

```tsx
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

1. Copy `ContactForm.tsx` into your components folder.
2. Render it with your form id and Turnstile site key.
3. Deploy on the verified domain: JSON posts are cross-origin, and the CORS preflight is answered for verified domains only. `localhost` is 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](/integrations/nextjs).
