React verbinden
Verwende ein normales Formular und sende dessen FormData. Die Empfangs-URL ist öffentlich. Private API-Zugangsdaten gehören nicht in Browsercode.
Absenden verarbeiten
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("Nachricht empfangen.");
if (pending.current === payload) { pending.current = null; form.reset(); }
} catch { setStatus("Der Empfang wurde nicht bestätigt. Deine Felder bleiben erhalten. Versuche es erneut."); }
finally { setBusy(false); }
}
return <form action="YOUR_FORM_URL" method="POST"
onSubmit={onSubmit} onInput={() => { pending.current = null; }}>
<label>E-Mail<input name="email" type="email" required /></label>
<label>Nachricht<textarea name="message" required /></label>
<button type="submit" disabled={busy}>Nachricht senden</button>
<p role="status">{status}</p>
</form>;
}Verbinde onSubmit mit deinem Formular. Setze pending.current bei onInput zurück, wenn jemand ein Feld ändert. Deaktiviere den Absende-Button, solange busy wahr ist. Zeige status in einem Element mit role="status" an.
Dein Design behalten
Eingabefelder benötigen name-Attribute. GlueForms akzeptiert gültige Felder, die nicht im gespeicherten Editor-Design vorkommen. Du musst weder Komponenten umbenennen noch dein CSS ersetzen.
Der Handler behält eine fehlgeschlagene Einsendung für einen erneuten Versuch. Er leert Felder erst nach bestätigtem Empfang und nur, wenn noch keine neue Nachricht begonnen wurde. Siehe Fehlerbehandlung mit JavaScript.