---
title: "Connect React"
canonical: "https://glueforms.com/docs/react"
language: "en"
description: "Add a receiving URL and a confirmed success state to a React form."
---

# Connect React

Use a normal form and submit its `FormData`. Keep the receiving URL public and private API credentials out of browser code.

## Add a submit handler

```javascript
import { useRef, useState } from 'react';

export function ContactForm() {
  const pending = useRef(null);
  const [busy, setBusy] = useState(false);
  const [status, setStatus] = useState('');
  async function onSubmit(event) {
    event.preventDefault();
    if (busy) return;
    const form = event.currentTarget;
    if (!pending.current) {
      pending.current = new FormData(form);
      pending.current.set('submission_id', crypto.randomUUID());
    }
    const payload = pending.current;
    setBusy(true);
    try {
      const response = await fetch(form.action, {
        method: 'POST', body: payload, credentials: 'omit',
        headers: { Accept: 'application/json' },
      });
      const receipt = await response.json();
      if (!response.ok || receipt.accepted !== true) throw new Error();
      setStatus("Message received.");
      if (pending.current === payload) { pending.current = null; form.reset(); }
    } catch { setStatus("Could not confirm receipt. Your fields are still here. Try again."); }
    finally { setBusy(false); }
  }
  return <form action="YOUR_FORM_URL" method="POST"
    onSubmit={onSubmit} onInput={() => { pending.current = null; }}>
    <label>Email<input name="email" type="email" required /></label>
    <label>Message<textarea name="message" required /></label>
    <button type="submit" disabled={busy}>Send message</button>
    <p role="status">{status}</p>
  </form>;
}
```

Attach `onSubmit` to your form. Set `onInput` to clear `pending.current` when the visitor edits a field. Disable your submit button while `busy` is true and render `status` in an element with `role="status"`.

## Keep your existing design

Your inputs need `name` attributes. GlueForms accepts valid fields even when they are absent from the saved builder design. You do not need to rename your components or replace your CSS.

The handler preserves a failed submission for retry. It clears fields only after confirmed receipt and only if the visitor has not started another message. See [JavaScript error handling](https://glueforms.com/docs/javascript).
