# Contact form for React

> One component that posts with fetch and shows the result inline. Works in Vite, Lovable and Bolt projects.

Last updated 2026-11-14. Web version: https://wrenform.com/docs/frameworks/react

```jsx
// src/ContactForm.jsx (React + Vite, also Lovable and Bolt projects)
import { useState } from "react";

export default function ContactForm() {
  const [status, setStatus] = useState("idle");

  async function onSubmit(e) {
    e.preventDefault();
    const form = e.currentTarget;
    setStatus("sending");
    try {
      const res = await fetch("https://wrenform.com/f/YOUR_FORM_ID", {
        method: "POST",
        headers: { Accept: "application/json" },
        body: new FormData(form),
      });
      setStatus(res.ok ? "sent" : "error");
      if (res.ok) form.reset();
    } catch {
      setStatus("error");
    }
  }

  return (
    <form onSubmit={onSubmit}>
      <label>Name <input type="text" name="name" autoComplete="name" required /></label>
      <label>Email <input type="email" name="email" autoComplete="email" required /></label>
      <label>Message <textarea name="message" required /></label>
      <input type="text" name="_honey" style={{ display: "none" }} tabIndex={-1} autoComplete="off" />
      <button type="submit" disabled={status === "sending"}>Send</button>
      <p role="status">
        {status === "sent" && "Thanks, your message was sent."}
        {status === "error" && "Something went wrong. Please try again."}
      </p>
    </form>
  );
}
```

## Steps

1. [Create a form](/sign-in) and copy its form ID, or let your AI tool create one [without an account](/docs/quick-forms).
2. Save the component as `src/ContactForm.jsx` (or `.tsx`) and replace `YOUR_FORM_ID`.
3. Render it where the form belongs: `import ContactForm from "./ContactForm";` then `<ContactForm />`. With React Router, give it its own `/contact` route.
4. Deploy. Then add your site's origin under **Allowed origins** in the form's settings.

**No backend needed.** The browser posts straight to Wrenform, so you don't need an API route, an edge function or an email service. The form ID is public by design; allowed origins stop other sites from using it.

To keep the ID in configuration, use a Vite variable: `import.meta.env.VITE_WRENFORM_ID`. Vite inlines it at build time.

## Lovable and Bolt

Both generate Vite and React projects, so the component works as is. Ask the tool to add it, or use the ready-made prompts on [Prompts for AI builders](/docs/ai). Tell the tool not to add a Supabase function or an email package for the form.

## Test it

Run `npm run dev`, open the page and send the form. You should see "Thanks, your message was sent.", the submission in your dashboard and an email. While testing on localhost, leave **Allowed origins** empty or add `http://localhost:5173`.

## Common problems

- **CORS error in the console.** The page's origin isn't in the form's allowed origins. Add it, with the port in development.
- **`empty_submission`.** An input lost its `name` attribute, so `FormData` is empty.
- **The success message never shows.** Check the request in the network tab: a `4xx` answer carries an error code, listed in [errors](/docs/errors).
- **Strict CSP.** Add `connect-src https://wrenform.com`.

## Next steps

- [My form isn't working](/docs/troubleshooting): symptom, cause and fix.
- [Spam](/docs/spam): the timing field and Turnstile.
- [Redirects](/docs/redirects): send visitors to a thank-you page instead.
- [Fields](/docs/fields): reserved fields, subjects and reply-to.
